上一页 1 2 3 4 5 6 7 8 ··· 20 下一页
摘要: int lengthOfLastWord(const char *s) { // Note: The Solution object is instantiated only once and is reused by each test case. const char* cur = s, *pStart; while(*cur==' ') //skip white space cur++; if(*cur=='\0') return 0; pStart = cur; ... 阅读全文
posted @ 2013-10-21 22:07 summer_zhou 阅读(101) 评论(0) 推荐(0) 编辑
摘要: 回溯法 vector > solveNQueens(int n) { // Note: The Solution object is instantiated only once and is reused by each test case. vector> res; vector queens; vector cols(n,false); vector diag(2*n,false); vector ardiag(2*n,false); dfs(0,n,queens,co... 阅读全文
posted @ 2013-10-19 16:54 summer_zhou 阅读(138) 评论(0) 推荐(0) 编辑
摘要: vector anagrams(vector &strs) { // Note: The Solution object is instantiated only once and is reused by each test case. unordered_map> mmap; vector res; for(vector::iterator it = strs.begin();it!=strs.end();it++) { string s = *it; ... 阅读全文
posted @ 2013-10-19 16:22 summer_zhou 阅读(131) 评论(0) 推荐(0) 编辑
摘要: 用回溯法求解。1.Permutations vector > permute(vector &num) { // Note: The Solution object is instantiated only once and is reused by each test case. vector> res; if(num.empty()) return res; vector bvisited(num.size(),false); vector permu; dfs(0,num,bv... 阅读全文
posted @ 2013-10-19 16:09 summer_zhou 阅读(210) 评论(0) 推荐(0) 编辑
摘要: 二分查找。要注意越界问题。以及除数是否为0的情况 int sqrt(int x) { // Note: The Solution object is instantiated only once and is reused by each test case. if(xmid) left = mid+1; else return mid; } return right; //返回的是right } 阅读全文
posted @ 2013-10-18 21:47 summer_zhou 阅读(158) 评论(0) 推荐(0) 编辑
摘要: 分两步走:1. 沿着反对角线做交换(i,j)--》(n-1-j,n-1-i)2. 沿着中间做交换 void rotate(vector > &matrix) { // Note: The Solution object is instantiated only once and is reused by each test case. if(matrix.empty()||matrix[0].empty()) return; int n = matrix.size(); for(int i=0;... 阅读全文
posted @ 2013-10-18 20:44 summer_zhou 阅读(132) 评论(0) 推荐(0) 编辑
摘要: 注意条件。 vector spiralOrder(vector > &matrix) { // Note: The Solution object is instantiated only once and is reused by each test case. vector res; if(matrix.empty()||matrix[0].empty()) return res; int m = matrix.size(); int n = matrix[0].size(); ... 阅读全文
posted @ 2013-10-18 19:59 summer_zhou 阅读(100) 评论(0) 推荐(0) 编辑
摘要: 注意一些边界条件 ListNode *rotateRight(ListNode *head, int k) { // Note: The Solution object is instantiated only once and is reused by each test case. if(head==NULL) return NULL; int len = getLength(head); k = k%len; if(k==0) return head; L... 阅读全文
posted @ 2013-10-18 17:02 summer_zhou 阅读(112) 评论(0) 推荐(0) 编辑
摘要: 不要忘了考虑这种情况[3,1] 此时left == mid,A[left]==A[mid] int search(int A[], int n, int target) { // Note: The Solution object is instantiated only once and is reused by each test case. int left = 0,right = n-1; while(leftA[mid]) //right part is sorted { if(A[m... 阅读全文
posted @ 2013-10-18 16:26 summer_zhou 阅读(111) 评论(0) 推荐(0) 编辑
摘要: vector > generateMatrix(int n) { // Note: The Solution object is instantiated only once and is reused by each test case. int start = 0; vector> res(n,vector(n)); if(n>& res) { int i; for(i=start;i=start;i--) res[end][i] = cnt++; ... 阅读全文
posted @ 2013-10-18 16:08 summer_zhou 阅读(86) 评论(0) 推荐(0) 编辑
上一页 1 2 3 4 5 6 7 8 ··· 20 下一页