- 博客(269)
- 资源 (1)
- 收藏
- 关注
原创 Leetcode: Linked List Cycle II
http://oj.leetcode.com/problems/linked-list-cycle-ii/ 参考了http://blog.youkuaiyun.com/whuwangyi/article/details/14103993 /** * Definition for singly-linked list. * struct ListNode { * int val; *
2014-01-29 00:39:51
886
原创 Leetcode: Permutations and Permutations II
http://oj.leetcode.com/problems/permutations/ class Solution { public: void Permute(vector &num, vector &used, int depth, vector> &res, vector ¤t){ if(depth==num.size()){
2013-10-30 08:35:27
1273
原创 Leetcode: Jump Game
http://oj.leetcode.com/problems/jump-game/ class Solution { public: // Don't code too fast // The bad dp algorithm below (commented out) has the complexity of O(N*N) /*bool search(i
2013-10-29 11:16:49
826
原创 Trapping Rain Water
http://oj.leetcode.com/problems/trapping-rain-water/ // Find the solution from the internet // For each individual slot, the capacity depends on the leftHighest and rightHighest // The naive s
2013-10-29 10:56:29
883
原创 Leetcode: Subsets II
http://oj.leetcode.com/problems/subsets-ii/ class Solution { public: // First, we sort the input set // Second, whenever we have a new element, we count the number of this element /
2013-10-29 09:19:27
730
原创 Leetcode: Search for a Range
http://oj.leetcode.com/problems/search-for-a-range/ class Solution { public: // Don't try to find one and then extend to the start direction and end direction // The time complexity wil
2013-10-29 08:56:22
784
原创 Leetcode: Minimum Depth of Binary Tree
http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ class Solution { public: // !!! // root==NULL doesn't indicate leaf node // root->left==root->right==NULL does indicate
2013-10-29 08:45:34
630
原创 Leetcode: Combinations
http://oj.leetcode.com/problems/combinations/ class Solution { private: vector used; vector > res; vector current; public: // It asks for all the combinations, so we need the va
2013-10-29 08:34:30
680
原创 Leetcode: Search in Rotated Sorted Array II
http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ class Solution { public: // The time complexity is O(N) in worest case // For example, A[]={1,1,1,1,1,1,1,1,1,1} and t
2013-10-29 08:33:00
686
原创 Leetcode: Search in Rotated Sorted Array
http://oj.leetcode.com/problems/search-in-rotated-sorted-array/ class Solution { public: // The key idea is that the start value and end value // can limite possible values of the array
2013-10-29 08:30:38
611
原创 Leetcode: 3Sum Closest
http://oj.leetcode.com/problems/3sum-closest/ class Solution { public: // Similar to 3Sum Equal // Enumerate the minimal number, and then it becomes similar to 2Sum Equal // The ove
2013-10-29 08:29:35
695
原创 Leetcode: Sum Root to Leaf Numbers
http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ // Tried many times to get accepted // Take care: // 1. root is NULL // 2. node with one child // 3. leaf class Solution { public:
2013-10-29 08:18:50
777
原创 Leetcode: Set Matrix Zeroes
http://oj.leetcode.com/problems/set-matrix-zeroes/ // For constant memory requirement, we normaly have two ways: // 1. Finding a way to use a few variables to represent the status // 2. Using t
2013-10-28 13:21:18
785
原创 Leetcode: Sort Colors
http://oj.leetcode.com/problems/sort-colors/ // The easy way is to use count sort, but it is going to need two-pass // The solution below need one-pass. // The idea is to keep all the numbers b
2013-10-28 12:56:36
676
原创 Leetcode: Remove Duplicates from Sorted Array II
http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ class Solution { public: int removeDuplicates(int A[], int n) { int current=0; for(int i=0;i<n;i++){
2013-10-28 11:31:29
556
原创 Leetcode: Single Number II
http://oj.leetcode.com/problems/single-number-ii/ #include "stdio.h" #include #include using namespace std; // Took more than two hours // Bit operation is not trival // Record the number of
2013-10-28 10:55:33
1005
原创 Leetcode: Remove Nth Node From End of List
http://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) :
2013-10-28 08:13:17
626
原创 Leetcode: Balanced Binary Tree
http://oj.leetcode.com/problems/balanced-binary-tree/ /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x
2013-10-17 06:58:30
703
原创 Leetcode: Integer to Roman
http://oj.leetcode.com/problems/integer-to-roman/ class Solution { public: string intToRoman(int num, int pos){ char *cc="IVXLCDM "; string res; char a=cc[pos*2],b=
2013-10-17 06:58:23
667
原创 Leetcode: Best Time to Buy and Sell Stock
http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/ class Solution { public: int maxProfit(vector &prices) { // Note: The Solution object is instantiated only once and
2013-10-17 06:58:19
865
原创 Leetcode: Search a 2D Matrix
http://oj.leetcode.com/problems/search-a-2d-matrix/ Take the matrix as a sorted list, and then use binary search directly. class Solution { public: int GetValue(vector > &matrix, int index
2013-10-17 06:58:13
626
原创 Leetcode: Container With Most Water
http://oj.leetcode.com/problems/container-with-most-water/ // It's not easy to get the O(N) solution directly. // The idea is to use this conclusion: if(height[i]=MaxArea(i+1,j)>=MaxArea(i,j-1)
2013-10-17 06:58:07
561
原创 Leetcode: Rotate Image
http://oj.leetcode.com/problems/rotate-image/ // Do it in place is not easy class Solution { public: void rotate(vector > &matrix) { int size=matrix.size(); for(int i=0;i<(s
2013-10-17 06:58:01
744
原创 Leetcode: Minimum Path Sum
http://oj.leetcode.com/problems/minimum-path-sum/ // The problem is straightforward, and the question is how to write it in an elegant way class Solution { public: const int INF=100000000;
2013-10-17 06:57:57
677
原创 Leetcode: Generate Parentheses
http://oj.leetcode.com/problems/generate-parentheses/ // Interesting questions // It is very easy to generate duplicated result with normal dp // The core idea is that from the start to the end
2013-10-17 06:57:21
661
原创 Leetcode: Swap Nodes in Pairs
http://oj.leetcode.com/problems/swap-nodes-in-pairs/ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(
2013-10-16 05:58:18
741
原创 Leetcode: Single Number
http://oj.leetcode.com/problems/single-number/ class Solution { public: int singleNumber(int A[], int n) { // Note: The Solution object is instantiated only once and is reused by ea
2013-10-16 05:57:33
905
原创 Leetcode: Roman to Integer
http://oj.leetcode.com/problems/roman-to-integer/ class Solution { public: int romanToInt(string s) { // Start typing your C/C++ solution below // DO NOT write int main() fu
2013-10-16 05:56:24
550
原创 Leetcode: Binary Tree Inorder Traversal
http://oj.leetcode.com/problems/binary-tree-inorder-traversal/ /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeN
2013-10-16 05:55:15
490
原创 Leetcode: Merge Two Sorted Lists
http://oj.leetcode.com/problems/merge-two-sorted-lists/ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), ne
2013-10-16 05:52:58
995
原创 Leetcode: Maximum Subarray
http://oj.leetcode.com/problems/maximum-subarray/ class Solution { public: int maxSubArray(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int ma
2013-10-16 05:51:46
659
原创 Leetcode: Populating Next Right Pointers in Each Node
http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/ /** * Definition for binary tree with next pointer. * struct TreeLinkNode { * int val; * TreeLinkNode *left, *
2013-10-16 05:50:29
619
原创 Leetcode: Remove Element
http://oj.leetcode.com/problems/remove-element/ class Solution { public: int removeElement(int A[], int n, int elem) { if(n==0) return 0; int start=0, end=n-1; while
2013-10-16 05:48:54
652
原创 Leetcode: Unique Binary Search Trees
http://oj.leetcode.com/problems/unique-binary-search-trees/ int DP[1000]; int dp(int num){ if(num==1||num==0) return 1; if(DP[num]>=0) return DP[num]; int res=0; for(int i=0;i<n
2013-10-16 05:42:53
500
原创 Leetcode: Convert Sorted Array to Binary Search Tree
http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right;
2013-10-16 05:40:40
529
原创 Leetcode: Remove Duplicates from Sorted Array
http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ class Solution { public: int removeDuplicates(int A[], int n) { // Start typing your C/C++ solution below
2013-10-16 05:37:36
473
原创 Leetcode: Search Insert Position
http://oj.leetcode.com/problems/search-insert-position/ class Solution { public: int searchInsert(int A[], int n, int target) { // Start typing your C/C++ solution below //
2013-10-16 05:36:41
537
原创 Leetcode: Remove Duplicates from Sorted List
http://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x)
2013-10-16 05:35:54
473
原创 Leetcode: Climbing Stairs
http://oj.leetcode.com/problems/climbing-stairs/ class Solution { public: int dp[10000]; int DP(int n){ if(dp[n]<0) dp[n]=DP(n-1)+DP(n-2); return dp[n]; } int cl
2013-10-16 05:34:50
497
原创 Leetcode: Merge Sorted Array
http://oj.leetcode.com/problems/merge-sorted-array/ class Solution { public: void merge(int A[], int m, int B[], int n) { int indexA=m-1; int indexB=n-1; int current
2013-10-16 05:33:40
511
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人