LeetCode
文章平均质量分 73
Dr-rong
这个作者很懒,什么都没留下…
展开
-
Trapping Rain Water
class Solution {public: int getArea(int upBound,int lowBound,int A[],int start,int end) { int res=0; for(int i=start;i<=end;++i) { if(A[i]>lowBound)原创 2015-01-07 10:33:17 · 296 阅读 · 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-01-24 18:52:11 · 250 阅读 · 0 评论 -
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-01-25 10:25:42 · 250 阅读 · 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-01-25 10:38:41 · 278 阅读 · 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-01-26 15:43:08 · 216 阅读 · 0 评论 -
Submission Details
class Solution {public: vector >res; void dfs(int start,int curSum,vector& path,const int& sz,const int& target,const vector& candidates) { if(curSum>target) {原创 2015-01-07 09:07:36 · 292 阅读 · 0 评论 -
Count and Say
class Solution {public: string tostr(int x) { vector tmp; while(x) { int i=x%10; tmp.push_back(i+'0'); x/=10; } ret原创 2015-01-06 12:13:33 · 247 阅读 · 0 评论 -
Triangle
基本的动态规划解法:class Solution {public: int minimumTotal(vector > &triangle) { if(triangle.size()==0) { return 0; } int MIN=(1<<15)-1; int原创 2015-01-27 13:40:52 · 250 阅读 · 0 评论 -
Multiply Strings
class Solution {public: vector partials; string multiplyChar(const string& num,char factor){ if(factor=='0'){ return ""; } if(factor=='1'){ ret原创 2015-01-08 13:44:48 · 260 阅读 · 0 评论 -
Best Time to Buy and Sell Stock II
解法一:动态规划第一版主要思想是填profit数组,profit [ i ]表示在第i天的最大的利润为多少,如果第i天没有卖出情况,则与profit[ i -1 ]相同,否则要找出自所有可能的上一个卖出日以来,最大的利润,当上一个卖出日为j时,日i的利润表示为profit [ i ] + prices[ i - 1 ] + dp[ j ][ i ](注意prices下标从0开始,所以要减一,其原创 2015-01-27 22:30:32 · 251 阅读 · 0 评论 -
Best Time to Buy and Sell Stock
class Solution {public: int maxProfit(vector &prices) { int n=prices.size(); if(n<2) { return 0; } vector minVal(n,0); int res=0;原创 2015-01-27 14:23:55 · 260 阅读 · 0 评论 -
Valid Palindrome
class Solution {public: char clearChar(char input) { if((input>='a')&&(input<='z')) { return input; } if((input>='0')&&(input<='9')) {原创 2015-01-28 15:41:55 · 196 阅读 · 0 评论 -
Best Time to Buy and Sell Stock III
class Solution {public: int maxProfit(vector &prices) { int n=prices.size(); if(n<2) { return 0; } vector dp(n+1,0); //dp[i] stands for原创 2015-01-28 15:28:21 · 284 阅读 · 0 评论 -
Pascal's Triangle
class Solution {public: vector > generate(int numRows) { vector > res; int rows=numRows-1; if(rows<0) { return res; } vector zero;原创 2015-01-26 15:16:07 · 269 阅读 · 0 评论 -
Populating Next Right Pointers in Each Node II
此题最naive的办法就是利用队列进行BFS,同一层的节点从左到右连接起来即可。但这样一来需要的额外空间至少是树的最大宽度。下面的解法需要的额外空间是树的高度,相比于树的宽度,树的高度一般情况来说不会太大,为logN级别。基本思路是维护每一层的第一个尚未连接起来的节点,当该层的一下个节点到来时,将其与新的节点连接起来,并替换为新的节点。/** * Definition for b原创 2015-01-26 16:38:57 · 234 阅读 · 0 评论 -
Longest Consecutive Sequence
题目中要求时间复杂度O(n),所以不能通过简单的排序来解决问题,但我试了下先排序再做的效果,很不错,至少以很漂亮的速度AC了,哈哈。。。先贴一个“投机取巧”先排序的版本,要注意去除重复元素的影响class Solution {public: int longestConsecutive(vector &num) { int n=num.size();原创 2015-01-29 14:36:31 · 265 阅读 · 0 评论 -
Surrounded Regions
本题看似简单,但处理不当也很容易犯错,比如我就犯错了。。。先贴我的代码版本一class Solution {public: vector rowDir; vector colDir; void fill(int row,int col,vector >& board) { int m=board.size(); int原创 2015-01-28 22:18:22 · 246 阅读 · 0 评论 -
Jump Game II
第一反应动态规划,代码如下,但是超时了class Solution {public: int jump(int A[], int n) { int* steps=new int[n]; steps[0]=0; for(int i=1;i<n;++i) { int minStep=原创 2015-01-11 14:50:41 · 941 阅读 · 0 评论 -
Clone Graph
/** * Definition for undirected graph. * struct UndirectedGraphNode { * int label; * vector neighbors; * UndirectedGraphNode(int x) : label(x) {}; * }; */class Solution {public:原创 2015-01-29 14:52:04 · 231 阅读 · 0 评论 -
Gas Station
感觉贪心的题目都比较难,不容易发现最优策略,经常是用其他方法解结果超时了才发现有贪心解法,关键是这时有了“此题一定有贪心解法”的心态,所以会专注于琢磨出贪心解法。本题的基本思路是维护一个截止目前为止所欠的油量,以及从一个有效位置开始,截止目前的冗余油量,当接下来冗余油量也不足以应付时,要及时更新有效为止,冗余油量以及所欠油量。最后再看最终的冗余油量能否弥补所欠的所有油量,如果可以,则返回有效位原创 2015-01-29 19:56:38 · 301 阅读 · 0 评论 -
Wildcard Matching
第一种方法,直接search,但是超时class Solution {public: bool ismatch(int sstart,int sLen,const char* s,int pstart,int pLen,const char* p) { //sequential * equals single *,so we remove the addit原创 2015-01-11 17:13:13 · 328 阅读 · 0 评论 -
Binary Tree Maximum Path Sum
弄清楚这道题中的path指的是什么是成功的关键。我就是在没弄清楚题意的情况下做了半天无用功。。。这里的path指的是从树中的一个节点开始,沿着边走到另一个节点位置,所经历的所有节点组成一个path,同一个节点不能在path中出现2次,其实就是图中的path。程序主体主需要对树进行一次dfs即可,在每个节点都要做一个决策:是否要从当前节点继续沿parent路径往上返回;如果返回,则最原创 2015-01-29 13:35:53 · 270 阅读 · 0 评论 -
Permutations
class Solution {public: vector > res; void mpermute(vector& perm,int pos,vector& num) { if(pos==num.size()) { res.push_back(perm); return;原创 2015-01-13 15:43:57 · 259 阅读 · 0 评论 -
Permutations II
class Solution {public: vector >res; int upperBound(int start,int end,int val,const vector& num) { while(start<end) { int mid=start+(end-start)/2;原创 2015-01-13 16:17:57 · 288 阅读 · 0 评论 -
Single Number II
根据经验,要求O(n)复杂度的算法多要用到hashmap,所以naive的版本一代码如下,平均需要n/3的额外存储空间class Solution {public: int singleNumber(int A[], int n) { if(n==0) { return -1; } map mma原创 2015-01-30 14:53:40 · 184 阅读 · 0 评论 -
Word Break II
class Solution {public: vector res; void dfs(int start,string& curString,const string& s,const vector >& dp,const unordered_set &dict) { if(start==s.size()) {原创 2015-01-31 19:57:39 · 304 阅读 · 0 评论 -
Candy
又是一道贪心题目,试了dfs,dp,divide-and-conquer多种做法,实在做不出来,看了tag提示要用贪心,又琢磨了半天终于算是做出来了。dfs和dp会在大数据时超时,divide-and-conquer在第21个测试用例时答案错了,但我暂时没看出来代码逻辑哪里有问题,我会把分而治之的代码贴在后边,欢迎同胞指正。先贴AC的贪心解法,关键是要认清楚两点:1.由于题目没原创 2015-01-31 14:32:00 · 358 阅读 · 0 评论 -
Word Break
class Solution {public: bool wordBreak(string s, unordered_set &dict) { int n=s.size(); if(n<2) { if(dict.count(s)) { return true;原创 2015-01-31 15:39:35 · 263 阅读 · 0 评论 -
Maximum Subarray
解法1:动态规划化class Solution {public: int maxSubArray(int A[], int n) { int* dp=new int[n](); dp[0]=A[0]; int max=dp[0]; for(int i=1;i<n;++i) { if原创 2015-01-14 13:54:01 · 224 阅读 · 0 评论 -
Rotate Image
class Solution {public: void rotate(vector > &matrix) { int n=matrix.size(); int l1=0; int l2=n-1; int dy[]={1,0,-1,0}; int dx[]={0,1,0,-1}; while(原创 2015-01-13 17:23:36 · 283 阅读 · 0 评论 -
Anagrams
class Solution {public: vector anagrams(vector &strs) { vector res; map > mmap; for(int i=0;i<strs.size();++i) { string tmp=strs[i]; sort(t原创 2015-01-13 19:45:29 · 215 阅读 · 0 评论 -
Spiral Matrix
class Solution {public: vector res; void visit(int rowStart,int rowEnd,int colStart,int colEnd,const vector >& matrix) { for(int i=colStart;i<colEnd;++i) { res原创 2015-01-14 12:32:37 · 237 阅读 · 0 评论 -
Jump Game
class Solution {public: bool canJump(int A[], int n) { int index=0; int beforeLimit=index+A[index]; if(beforeLimit>=(n-1)) { return true; }原创 2015-01-14 11:42:54 · 266 阅读 · 0 评论 -
Unique Paths
class Solution {public: int uniquePaths(int m, int n) { int dp[101][101]; memset(dp,0,sizeof(dp)); for(int i=1;i<=n;++i) { dp[1][i]=1; }原创 2015-01-16 11:17:40 · 222 阅读 · 0 评论 -
Length of Last Word
class Solution {public: int lengthOfLastWord(const char *s) { int len=strlen(s); int start=0; int end=0; bool wordStart=false; bool wordEnd=false;原创 2015-01-15 10:37:07 · 241 阅读 · 0 评论 -
Linked List Cycle
基本思想是要能够知道哪些节点是已经访问过的,这样如果再次访问已经访问过的节点,就是存在环的证明。如果用naive的想法,我们可以用一个set来记录已经访问过的节点,每当访问一个新的节点时,去查看是否已经存在于set中即可,这样平均需要消耗额外O(N)的空间。如果想要省去这部分额外开销,必须从节点本身来考虑,能够改变节点的某些“特质”,使得我们能够认出已经访问过的节点。改变无非发生在两个地方,v原创 2015-02-03 10:58:47 · 238 阅读 · 0 评论 -
Minimum Path Sum
class Solution {public: int minPathSum(vector > &grid) { int m=grid.size(); if(m==0) { return 0; } int n=grid[0].size(); if(n==0)原创 2015-01-16 20:00:08 · 214 阅读 · 0 评论 -
Simplify Path
class Solution {public: string simplifyPath(string path) { stack fileName; string curFileName; for(int i=0;i<path.size();++i) { if(path[i]=='/')原创 2015-01-17 10:24:59 · 239 阅读 · 0 评论 -
N-Queens
最naive的解法,枚举n的排列,然后判断每个排列是否符合规则要求,但是会超时(n=9时)class Solution {public: vector > res; bool isValide(const vector& pos) { for(int i=0;i<pos.size();++i) { for(int原创 2015-01-15 10:08:45 · 346 阅读 · 0 评论 -
N-Queens II
class Solution {public: Solution():cnt(0){} int cnt; bool isValide(const vector& pos) { for(int i=0;i<pos.size();++i) { for(int j=i+1;j<pos.size();++j)原创 2015-01-15 10:17:25 · 248 阅读 · 0 评论