
LeetCode
hjhomw
今天的苦逼是为了不这样一直苦逼下去,坚持,坚持,坚持!
展开
-
Sum Root to Leaf Numbers
Description:Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the原创 2016-08-26 21:00:01 · 420 阅读 · 0 评论 -
Wildcard Matching
Description:Implement wildcard pattern matching with support for ‘?’ and ‘*’. ‘?’ Matches any single character. ‘*’ Matches any sequence of characters (including the empty sequence). The matching sho原创 2016-07-21 19:30:24 · 443 阅读 · 0 评论 -
Simplify Path
Description:Given an absolute path for a file (Unix-style), simplify it. For example, path = “/home/”, => “/home” path = “/a/./b/../../c/”, => “/c”Corner Cases: • Did you consider the case where pa原创 2016-07-25 19:10:35 · 226 阅读 · 0 评论 -
Length of Last Word
Description:Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of last word in the string. If the last word does not exist, return 0. Note: A w原创 2016-07-25 19:39:36 · 215 阅读 · 0 评论 -
Valid Parentheses
Description:Given a string containing just the characters ’(’, ’)’, ’{’, ’}’, ’[’ and ’]’, determine if the input string is valid. The brackets must close in the correct order, ”()” and ”()[]” are al原创 2016-07-27 09:24:06 · 216 阅读 · 0 评论 -
Longest Valid Parentheses
Description: Given a string containing just the characters ’(’ and ’)’, find the length of the longest valid (wellformed) parentheses substring. For ”(()”, the longest valid parentheses substring is原创 2016-07-27 09:59:07 · 201 阅读 · 0 评论 -
Largest Rectangle in Histogram
Description:Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. 分析: 参考这两篇博文 http://blog.csdn原创 2016-07-27 15:54:22 · 182 阅读 · 0 评论 -
Evaluate Reverse Polish Notation
Description:Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: [“2”, “1”,原创 2016-07-27 16:58:28 · 176 阅读 · 0 评论 -
Binary Tree Postorder Traversal
Description:Given a binary tree, return the postorder traversal of its nodes’ values. For example: Given binary tree {1,#,2,3}, return [3,2,1]. Note: Recursive solution is trivial, could you do it原创 2016-07-30 21:27:45 · 198 阅读 · 0 评论 -
Longest Common Prefix
Description:Write a function to find the longest common prefix string amongst an array of strings.分析: 先将第一个字符串与第二个字符串比较,找出公共部分,再与第三个字符串比较,依次类推。。。代码:#include <iostream>#include <vector>#include <stri原创 2016-07-23 16:05:33 · 275 阅读 · 0 评论 -
Valid Number
Description:Validate if a given string is numeric. Some examples: “0” => true ” 0.1 ” => true “abc” => false “1 a” => false “2e10” => true Note: It is intended for the problem statement to be am原创 2016-07-23 18:03:37 · 302 阅读 · 0 评论 -
String to Integer (atoi)
分析 细节题。注意几个测试用例: 1. 不规则输入,但是有效, ”-3924x8fc”, ” + 413”, 2. 无效格式, ” ++c”, ” ++1” 3. 溢出数据, ”2147483648”代码:#include <iostream>#include <sstream>#include <string.h>#include <limits.h>using namespace原创 2016-04-24 21:37:43 · 277 阅读 · 0 评论 -
Add Binary
Given two binary strings, return their sum (also a binary string). For example, 62 第 3 章 字符串 a = “11” b = “1” Return ”100”.代码:#include <iostream>#include <algorithm>using namespace std;string Add原创 2016-04-24 22:11:11 · 294 阅读 · 0 评论 -
Anagrams
Description:Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case.分析: Anagram(回文构词法)是指打乱字母顺序从而得到新的单词,比如 “dormitory” 打乱字母顺 序会变成 “dirty room原创 2016-07-25 12:17:15 · 342 阅读 · 0 评论 -
Longest Palindromic Substring
Description: Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exist one unique longest palindromic substring.分析见:原创 2016-07-21 08:22:59 · 184 阅读 · 0 评论 -
Count and Say
Description:The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, … 1 is read off as “one 1” or 11. 11 is read off as “two 1s” or 21. 21 is read off原创 2016-07-25 10:34:17 · 227 阅读 · 0 评论 -
Integer to roman
Description: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.分析:每次选择能表示的最大值,把对应的字符串连起来代码:#include <iostream>using namespace std; string Inte原创 2016-07-23 22:36:23 · 207 阅读 · 0 评论 -
Roman to Integer
Description:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.分析: 1~9: {“I”, “II”, “III”, “IV”, “V”, “VI”, “VII”, “VIII”, “IX”};10~90: {“X”, “原创 2016-07-23 21:51:58 · 270 阅读 · 0 评论 -
Regular Expression Matching
Description:Implement regular expression matching with support for ‘.’ and ‘*’. ‘.’ Matches any single character. ‘*’ Matches zero or more of the preceding element. The matching should cover the enti原创 2016-07-21 16:46:06 · 215 阅读 · 0 评论 -
Binary Tree Inorder Traversal
Description:Given a binary tree, return the inorder traversal of its nodes’ values. For example: Given binary tree {1,#,2,3}, return [1,3,2]. Note: Recursive solution is trivial, could you do it i原创 2016-07-30 17:58:31 · 191 阅读 · 0 评论 -
Binary Tree Preorder Traversal
Description:Given a binary tree, return the preorder traversal of its nodes’ values. For example: Given binary tree {1,#,2,3},return [1,2,3]. Note: Recursive solution is trivial, could you do it iter原创 2016-07-30 17:04:49 · 218 阅读 · 0 评论 -
Convert Sorted List to Binary Search Tree
Description: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.代码:#include <iostream>#include <vector>#include <stack>#define Elementype in原创 2016-08-25 20:11:38 · 230 阅读 · 0 评论 -
Convert Sorted Array to Binary Search Tree
Description:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.代码:#include <iostream>#include <vector>#include <stack>#define Elementype intusing namespa原创 2016-08-25 17:01:29 · 256 阅读 · 0 评论 -
Minimum Depth of Binary Tree
Description:Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.代码:#include <iostream>#incl原创 2016-08-25 22:11:29 · 236 阅读 · 0 评论 -
Maximum Depth of Binary Tree
Description:Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.分析: 同上题代码:#include <iostrea原创 2016-08-25 22:38:30 · 332 阅读 · 0 评论 -
Path Sum
Description:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tr原创 2016-08-26 09:38:31 · 234 阅读 · 0 评论 -
Path Sum II
Desciption:Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum. For example: Given the below binary tree and sum = 22, return代码:#include <iostream>#i原创 2016-08-26 10:31:15 · 240 阅读 · 0 评论 -
Binary Tree Maximum Path Sum
Description:Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree,Return 6.分析: 这题很难,路径可以从任意节点开始,到任意节点结束。可以利用“最大连转载 2016-08-26 18:23:05 · 618 阅读 · 0 评论 -
Populating Next Right Pointers in Each Node
Description:Given a binary tree struct TreeLinkNode { int val; TreeLinkNode *left, *right, *next; TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {} };Populate each next pointer原创 2016-08-26 20:42:30 · 500 阅读 · 0 评论 -
Validate Binary Search Tree
Description:Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: • The left subtree of a node contains only nodes with keys less than the node原创 2016-08-25 16:01:11 · 225 阅读 · 0 评论 -
Unique Binary Search Trees II
Description: Given n, generate all structurally unique BST’s (binary search trees) that store values 1…n. For example, Given n = 3, your program should return all 5 unique BST’s shown below. 代码:#inc原创 2016-08-25 15:41:27 · 193 阅读 · 0 评论 -
Binary Tree Level Order Traversal II
Description:Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,原创 2016-07-31 10:54:51 · 213 阅读 · 0 评论 -
Binary Tree Level Order Traversal
Description:Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, return its level o原创 2016-07-31 10:45:17 · 195 阅读 · 0 评论 -
Binary Tree Zigzag Level Order Traversal
Description: Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between). For example: Gi原创 2016-08-24 09:10:25 · 216 阅读 · 0 评论 -
Same Tree
Description:Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value.代码:#i原创 2016-08-24 10:37:00 · 228 阅读 · 0 评论 -
Construct Binary Tree from Preorder and Inorder Traversal
Description:Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree.分析: 先序遍历和中序遍历可以确定一颗二叉树。思想方法来自于网上#include <iostream原创 2016-08-24 20:36:24 · 211 阅读 · 0 评论 -
Construct Binary Tree from Inorder and Postorder Traversal
Description:Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree.分析:代码:#include <iostream>#include <vector>#inclu原创 2016-08-24 21:56:24 · 305 阅读 · 0 评论 -
Balanced Binary Tree
Description:Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node n原创 2016-08-24 15:23:05 · 233 阅读 · 0 评论 -
Unique Binary Search Trees
描述 Given n, how many structurally unique BST’s (binary search trees) that store values 1…n? For example, Given n = 3, there are a total of 5 unique BST’s.分析: 这方法从网上找到的,简单清晰易懂。如果把上例的顺序改一下,就可以看出规律了。比如原创 2016-08-25 09:57:07 · 262 阅读 · 0 评论 -
Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, ”A man, a plan, a canal: Panama” is a palindrome. ”race a car” is not a pa原创 2016-04-23 16:36:24 · 615 阅读 · 0 评论