摘要: ListNode* reverseBetween(ListNode* prev,ListNode* next)//reverse prev->next, last->prev之间的链表 { ListNode* last = prev->next; ListNode* cur = last->next; while(cur!=next) { last->next = cur->next; cur->next = prev->next; prev->next = ... 阅读全文
posted @ 2013-10-26 21:18 summer_zhou 阅读(152) 评论(0) 推荐(0) 编辑
摘要: 有个公式。假设集合为[1,2,3,4],求出第6个组合。第6个组合对应的下标为5(下标从0开始),我们首先求出5所对应的lehmer码(lehmer code的解释参考链接1):5/3! = 0 余55/2! = 2 余11/1! = 1 余00 (lehmer code最后一位总为0)所以所求lehmer码为02105 = 0*3!+2*2!+1*1!+0*0! (0210)当前集合对应的序列为1234接下来将lehmer码中的每个数字当做当前序列的下标,下标0对应的集合元素为1,当前序列变成234;下标2对应的集合元素为4,当前序列变成23;下标1对应的集合元素为3,当前序列变成2;下标0 阅读全文
posted @ 2013-10-26 17:53 summer_zhou 阅读(232) 评论(0) 推荐(0) 编辑
摘要: 和minimum window string的原理一样。维护一个window vector findSubstring(string S, vector &L) { // Start typing your C/C++ solution below // DO NOT write int main() function vector res; if(S.empty()||L.empty()) return res; unordered_map dict; vector needed(... 阅读全文
posted @ 2013-10-26 16:24 summer_zhou 阅读(161) 评论(0) 推荐(0) 编辑
摘要: O(n)的解法。[i,j]维护一个window,当cnt==tlen时表示这是一个valid window. Incrementcntby one if hasFound[x] is less than or equal to needToFind[x]. Why? When the constraint is met (that is,countequals toT's size), we immediately advance begin pointer as far right as possible while maintaining the constraint.needed 阅读全文
posted @ 2013-10-26 13:46 summer_zhou 阅读(218) 评论(0) 推荐(0) 编辑
摘要: int candy(vector &ratings) { // Note: The Solution object is instantiated only once and is reused by each test case. if(ratings.empty()) return 0; int n = ratings.size(); vector candys(n,1); int next_candy = 2; for(int i=0;i0&&ratings[i]>ratin... 阅读全文
posted @ 2013-10-25 22:25 summer_zhou 阅读(130) 评论(0) 推荐(0) 编辑
摘要: 如何避免多个0的情况。。。。 vector restoreIpAddresses(string s) { // Note: The Solution object is instantiated only once and is reused by each test case. vector res; string tmp; dfs(0,4,s,tmp,res); return res; } void dfs(int curpos,int cnt,string& s, string tmp,vec... 阅读全文
posted @ 2013-10-25 21:09 summer_zhou 阅读(109) 评论(0) 推荐(0) 编辑
摘要: 本质上是动态规划。解法一: //O(m*m*n) int maximalRectangle(vector > &matrix) { if(matrix.empty()||matrix[0].empty()) return 0; int m = matrix.size(); int n = matrix[0].size(); vector> cnt(m,vector(n,0)); //cnt[i][j]表示matrix[i][j]往左边延伸‘1’的最大个数。 i... 阅读全文
posted @ 2013-10-25 20:51 summer_zhou 阅读(185) 评论(0) 推荐(0) 编辑
摘要: 计算 int largestRectangleArea(vector &height) { // Start typing your C/C++ solution below // DO NOT write int main() function stack st; height.push_back(0); //加入0之后,起哨兵的作用,这样即使矩形是递增的,也可以将面积计算出来(会走到else语句) int max_area = 0; int i=0; w... 阅读全文
posted @ 2013-10-25 19:31 summer_zhou 阅读(195) 评论(0) 推荐(0) 编辑
摘要: 好多特殊情况。第二次做还是错了很多次,要好好考虑各种情况。 int numDecodings(string s) { // Note: The Solution object is instantiated only once and is reused by each test case. if(s.empty()||s[0]=='0') return 0; int cnt1 = 1, cnt2 = 1; int cnt = 1; //注意,初始时1. for(int i=1;i='1'&&s... 阅读全文
posted @ 2013-10-25 15:59 summer_zhou 阅读(158) 评论(0) 推荐(0) 编辑
摘要: 三维DP。重复子问题->DP bool isScramble(string s1, string s2) { // Note: The Solution object is instantiated only once and is reused by each test case. if(s1.size()!=s2.size()) return false; if(s1.empty()) return true; int n = s1.size(); vec... 阅读全文
posted @ 2013-10-25 15:30 summer_zhou 阅读(131) 评论(0) 推荐(0) 编辑