leetcode
文章平均质量分 62
美如画是我
这是一位Leader的博客
展开
-
Binary Tree Level Order Traversal
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti转载 2015-04-29 11:11:06 · 235 阅读 · 0 评论 -
Binary Tree Inorder Traversal
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti转载 2015-04-29 11:11:37 · 321 阅读 · 0 评论 -
Binary Tree Zigzag Level Order Traversal
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti转载 2015-04-29 11:20:13 · 257 阅读 · 0 评论 -
Unique Paths II
class Solution {public: int uniquePathsWithObstacles(vector>& obstacleGrid) { const int m=obstacleGrid.size(); const int n=obstacleGrid[0].size(); if(obstacl原创 2015-05-08 16:01:20 · 270 阅读 · 0 评论 -
Decode Ways
class Solution {public: int numDecodings(string s) { const int size=s.size(); vector dp(size+1); //dp[i]代表到第i个符号有多少种解码方式 dp[0]=1; //初始化dp[0]为1,为了计算的需要 i原创 2015-05-08 11:37:50 · 237 阅读 · 0 评论 -
House Robber
class Solution {public: int rob(vector& nums) { const int size=nums.size(); int maxRob=INT_MIN; if(size==0) return 0; else if(size==1)转载 2015-05-07 22:26:22 · 232 阅读 · 0 评论 -
Unique Binary Search Trees II
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas转载 2015-05-04 11:49:29 · 270 阅读 · 0 评论 -
Maximum Subarray
class Solution {public: int maxSubArray(vector& nums) { const int n=nums.size(); int maxSum=0; //每一段的最大值,局部最优 int result=INT_MIN; //全局最优 for(in转载 2015-05-07 22:02:00 · 271 阅读 · 0 评论 -
Unique Binary Search Trees
class Solution {public: int numTrees(int n) { vector f(n+1,0); f[0]=1; f[1]=1; for(int i=2;i<=n;++i) { for(int k=0;k<=i-1;++k)转载 2015-05-04 11:22:35 · 252 阅读 · 0 评论 -
Word Break II
class Solution {public: vector wordBreak(string s, unordered_set& wordDict) { const int n=s.length(); vector result(n,false); for(int i=0;i<n;++i) {转载 2015-05-09 22:22:21 · 385 阅读 · 0 评论 -
Balanced Binary Tree
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti转载 2015-04-30 16:23:00 · 368 阅读 · 0 评论 -
Maximum Product Subarray
class Solution {public: int maxProduct(vector& nums) { const int size=nums.size(); if(size==0) return 0; int maxLast=nums[0]; int minLast=转载 2015-05-07 23:03:08 · 276 阅读 · 0 评论 -
Triangle
class Solution {public: int minimumTotal(vector > &triangle) { if(!triangle.size()) return 0; vector dp(triangle.size()); int minT=INT_MAX; dp[0]=tria转载 2015-05-07 17:33:01 · 251 阅读 · 0 评论 -
Unique Paths
class Solution {public: int uniquePaths(int m, int n) { vector> dp(m,vector(n,1)); //dp[i][j] 表示到第i行,第j列的路径条数,初始化为1的目的在于初始化边框上的数值 for(int i=1;i<m;++i) for(int j=原创 2015-05-08 15:36:59 · 260 阅读 · 0 评论 -
Best Time to Buy and Sell Stock
class Solution {public: int maxProfit(vector& prices) { const int size=prices.size(); if(size==0) return 0; int curMin=prices[0]; int MaxPro=0;转载 2015-05-08 10:09:52 · 249 阅读 · 0 评论 -
Symmetric Tree
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti转载 2015-04-30 13:56:52 · 302 阅读 · 0 评论 -
Flatten Binary Tree to Linked List
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti转载 2015-04-30 16:38:51 · 262 阅读 · 0 评论 -
Best Time to Buy and Sell Stock II
class Solution {public: int maxProfit(vector &prices) { if(prices.empty()) return 0; int max=0; int start=prices[0]; int last=prices转载 2015-05-09 22:50:12 · 267 阅读 · 0 评论 -
Recover Binary Search Tree
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti转载 2015-04-30 17:17:35 · 275 阅读 · 0 评论 -
Binary Tree Level Order Traversal II
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti转载 2015-04-29 11:08:41 · 213 阅读 · 0 评论 -
Same Tree
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti转载 2015-04-30 13:51:46 · 250 阅读 · 0 评论 -
Validate Binary Search Tree
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti转载 2015-04-30 16:14:02 · 282 阅读 · 0 评论 -
Populating Next Right Pointers in Each Node
/** * Definition for binary tree with next pointer. * struct TreeLinkNode { * int val; * TreeLinkNode *left, *right, *next; * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL)转载 2015-04-30 18:38:50 · 223 阅读 · 0 评论 -
Best Time to Buy and Sell Stock III
1、分别计算出0天到i天的最大利润和i天到最后一天的最大利润,两者相加,找出最大的,但是超时了。class Solution {public: int maxProfit(vector& prices) { const int size=prices.size(); if(size==0) return 0;原创 2015-05-09 23:25:30 · 246 阅读 · 0 评论 -
Best Time to Buy and Sell Stock
class Solution {public: int maxProfit(vector& prices) { const int len=prices.size(); if(len==0) return 0; int MaxPro=0; int curMin=prices[0];转载 2015-05-04 16:09:33 · 197 阅读 · 0 评论 -
Maximum Depth of Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas转载 2015-05-04 14:31:51 · 185 阅读 · 0 评论 -
Sum Root to Leaf Numbers
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas转载 2015-05-04 15:43:52 · 223 阅读 · 0 评论 -
Word Ladder
class Solution {public: int ladderLength(string beginWord, string endWord, unordered_set& wordDict) { if(beginWord.size()!=endWord.size()) return 0; if(beginWord.转载 2015-05-04 15:48:40 · 382 阅读 · 0 评论 -
Convert Sorted List to Binary Search Tree
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; *//** * Definition for binary tree * stru转载 2015-05-01 21:20:05 · 342 阅读 · 0 评论 -
Linked List Cycle
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: bo转载 2015-05-04 21:11:08 · 217 阅读 · 0 评论 -
Linked List Cycle II
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: Li转载 2015-05-04 22:06:22 · 255 阅读 · 0 评论 -
Minimum Depth of Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas转载 2015-05-04 14:33:19 · 236 阅读 · 0 评论 -
Minimum Path Sum
class Solution {public: int minPathSum(vector>& grid) { if(grid.empty()||grid[0].empty()) return 0; int m=grid.size(); int n=grid[0].size(); vect转载 2015-05-04 15:24:29 · 236 阅读 · 0 评论 -
Populating Next Right Pointers in Each Node II
/** * Definition for binary tree with next pointer. * struct TreeLinkNode { * int val; * TreeLinkNode *left, *right, *next; * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL)转载 2015-04-30 23:40:43 · 246 阅读 · 0 评论 -
Construct Binary Tree from Preorder and Inorder Traversal
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti转载 2015-05-01 20:43:20 · 250 阅读 · 0 评论 -
Convert Sorted Array to Binary Search Tree
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti原创 2015-05-01 20:56:46 · 309 阅读 · 0 评论 -
Construct Binary Tree from Inorder and Postorder Traversal
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti转载 2015-05-01 20:45:02 · 254 阅读 · 0 评论 -
Valid Palindrome
class Solution {public: bool isPalindrome(string s) { if(s.size()==0) return true; const int len=s.size(); int start=0; int end=len-1;转载 2015-05-06 16:07:22 · 255 阅读 · 0 评论 -
Add Binary
class Solution {public: string addBinary(string a, string b) { const int sizeofa=a.size(); const int sizeofb=b.size(); if(sizeofa<sizeofb) return addBina转载 2015-05-06 19:07:22 · 234 阅读 · 0 评论 -
3Sum
class Solution {public: vector> threeSum(vector& nums) { vector> result; vector cur; int len=nums.size(); if(len<3) return result;转载 2015-05-06 21:37:14 · 227 阅读 · 0 评论