摘要: DP bool wordBreak(string s, unordered_set &dict) { // Note: The Solution object is instantiated only once and is reused by each test case. //dp if(dict.find(s)!=dict.end()) return true; int n = s.size(); vector> dp(n,vector(n,false)); int i,j,k... 阅读全文
posted @ 2013-10-06 23:22 summer_zhou 阅读(128) 评论(0) 推荐(0) 编辑
摘要: RandomListNode *copyRandomList(RandomListNode *head) { // Note: The Solution object is instantiated only once and is reused by each test case. //three steps//1. 为原链表中每个节点创建一个shallow copy,用next指针指向这个copy节点。//2. 利用原链表的next指针,将新链表的random指针指向正确的节点。//3. 将两个链表分开。 if(!head) ... 阅读全文
posted @ 2013-10-06 22:55 summer_zhou 阅读(164) 评论(0) 推荐(0) 编辑
摘要: Q:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0A: 在有序数组中找到等于tar 阅读全文
posted @ 2013-10-06 22:26 summer_zhou 阅读(162) 评论(0) 推荐(0) 编辑
摘要: 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 all valid but"(]"and"([)]"are not.A: 阅读全文
posted @ 2013-10-06 17:20 summer_zhou 阅读(153) 评论(0) 推荐(0) 编辑
摘要: ListNode *swapPairs(ListNode *head) { // Note: The Solution object is instantiated only once and is reused by each test case. ListNode *prev,*cur,*next; if(!head||!head->next) return head; ListNode* newhead,*pprev; prev = head; cur = prev->nex... 阅读全文
posted @ 2013-10-06 14:45 summer_zhou 阅读(125) 评论(0) 推荐(0) 编辑
摘要: vector generateParenthesis(int n) { // Note: The Solution object is instantiated only once and is reused by each test case. int left_cnt = n,right_cnt = n; vector res; dfs(left_cnt,right_cnt,"",res); return res; } void dfs(int left_cnt,int right_cnt,s... 阅读全文
posted @ 2013-10-06 14:22 summer_zhou 阅读(119) 评论(0) 推荐(0) 编辑
摘要: ListNode *removeNthFromEnd(ListNode *head, int n) { // Note: The Solution object is instantiated only once and is reused by each test case. if(!head||nnext; while(fast->next) { fast = fast->next; prev = slow; slow = slow->next; ... 阅读全文
posted @ 2013-10-06 14:09 summer_zhou 阅读(131) 评论(0) 推荐(0) 编辑
摘要: int uniquePathsWithObstacles(vector > &obstacleGrid) { // Start typing your C/C++ solution below // DO NOT write int main() function if(obstacleGrid[0][0]==1) return 0; int m = obstacleGrid.size(); int n = obstacleGrid[0].size(); vector> dp(m,... 阅读全文
posted @ 2013-10-06 13:48 summer_zhou 阅读(125) 评论(0) 推荐(0) 编辑
摘要: bool isPalindrome(int x) { // Note: The Solution object is instantiated only once and is reused by each test case. if(x=10) { div*=10; } while(x) { int a = x/div; int b = x%10; if(a!=b) re... 阅读全文
posted @ 2013-10-06 09:48 summer_zhou 阅读(102) 评论(0) 推荐(0) 编辑
摘要: 考虑越界的问题 int reverse(int x) { // Note: The Solution object is instantiated only once and is reused by each test case. bool bNega = false; if(xINT_MAX) return (bNega?INT_MIN:INT_MAX); else return (bNega?-res:res); } 阅读全文
posted @ 2013-10-06 09:25 summer_zhou 阅读(166) 评论(0) 推荐(0) 编辑