2013年9月3日

remove duplicates from sorted array

摘要: 1 class Solution { 2 public: 3 int removeDuplicates(int A[], int n) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int step = 0; 7 if( n 0 && A[i] != A[i-1])14 A[i-step] = A[i];15 16 }1... 阅读全文

posted @ 2013-09-03 22:46 jumping_grass 阅读(151) 评论(0) 推荐(0)

remove elements

摘要: 1 class Solution { 2 public: 3 int removeElement(int A[], int n, int elem) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int step = 0; 7 for(int i=0;i 0 && A[i]!=elem)10 A[i-step] = A[i];11 if( A... 阅读全文

posted @ 2013-09-03 22:37 jumping_grass 阅读(155) 评论(0) 推荐(0)

permutation sequence

摘要: 1 class Solution { 2 public: 3 string getPermutation(int n, int k) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 if( k flag(n, 0 ); 9 for( int i=0;i=1;i--)13 N *= i;14 if( k > N ) k = k % N;15 16 ... 阅读全文

posted @ 2013-09-03 21:45 jumping_grass 阅读(211) 评论(0) 推荐(0)

rotate list

摘要: 1 class Solution { 2 public: 3 ListNode *rotateRight(ListNode *head, int k) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 if( head == NULL || k == 0 ) return head; 7 ListNode * root = new ListNode (0); 8 root -> nex... 阅读全文

posted @ 2013-09-03 21:18 jumping_grass 阅读(139) 评论(0) 推荐(0)

unique path2

摘要: 1 class Solution { 2 public: 3 int helper(vector > &G, vector > &S,int r, int c ) 4 { 5 if( r == G.size()-1 && c == G[0].size()-1 && G[r][c]!=1 ) return 1; 6 if( r = G.size() || c = G[0].size() || G[r][c] == 1) 7 return 0; 8 if( S[r][c] != -1 ) return S... 阅读全文

posted @ 2013-09-03 20:54 jumping_grass 阅读(185) 评论(0) 推荐(0)

search a 2d box

摘要: 1 class Solution { 2 public: 3 bool searchMatrix(vector > &matrix, int target) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 if(matrix.empty() || matrix[0].empty() ) return false; 7 int left = 0; 8 int right = matri... 阅读全文

posted @ 2013-09-03 19:43 jumping_grass 阅读(129) 评论(0) 推荐(0)

sort colors

摘要: 1 class Solution { 2 public: 3 void sortColors(int A[], int n) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int left = 0; 7 int right = n-1; 8 9 int i=0;10 int j = n-1;11 12 while( ... 阅读全文

posted @ 2013-09-03 19:33 jumping_grass 阅读(139) 评论(0) 推荐(0)

word search

摘要: 1 class Solution { 2 public: 3 bool travel(vector > &board,int r,int c, 4 vector > &flag, string word,int pos) 5 { 6 if( pos == word.length() ) return true; 7 if( r=board.size() || c=board[0].size()) 8 return false; 9 if(!flag[r][c] || word[po... 阅读全文

posted @ 2013-09-03 17:05 jumping_grass 阅读(107) 评论(0) 推荐(0)

partion list

摘要: 1 class Solution { 2 public: 3 ListNode *partition(ListNode *head, int x) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 if( !head ) return NULL; 7 8 ListNode* ptr = new ListNode(0); 9 ptr->next = head;10 ... 阅读全文

posted @ 2013-09-03 16:39 jumping_grass 阅读(142) 评论(0) 推荐(0)

grey code

摘要: 1 class Solution { 2 public: 3 vector grayCode(int n) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 7 8 vector vec(pow(2.0,n),0); 9 for(int i=1;i=pow(2.0,i-1); j-- )12 {13 vec[j... 阅读全文

posted @ 2013-09-03 15:38 jumping_grass 阅读(122) 评论(0) 推荐(0)

decode ways

摘要: 1 class Solution { 2 public: 3 4 bool check(string s) 5 { 6 if( s.length() == 1 ) 7 return s>="1" && s = "10" && s = s.length() ) return 1;16 17 if( left == right) 18 {19 if(s[left]>='1'&&s[left] right ) return 1;25 26 ... 阅读全文

posted @ 2013-09-03 15:27 jumping_grass 阅读(139) 评论(0) 推荐(0)

subsets 2

摘要: 1 class Solution { 2 public: 3 vector > vec; 4 void subsets(vector &S,vector &v, int start) 5 { 6 vec.push_back(v); 7 for(int i=start;i > subsetsWithDup(vector &S) {17 // Start typing your C/C++ solution below18 // DO NOT write int main() function19 ... 阅读全文

posted @ 2013-09-03 14:57 jumping_grass 阅读(149) 评论(0) 推荐(0)

reverse linked list2

摘要: 1 class Solution { 2 public: 3 ListNode *reverseBetween(ListNode *head, int m, int n) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 ListNode * root = new ListNode(0); 7 root->next = head; 8 ListNode* ptr = root; 9 ... 阅读全文

posted @ 2013-09-03 14:40 jumping_grass 阅读(161) 评论(0) 推荐(0)

restore IP addresses

摘要: 1 class Solution { 2 private: 3 vector ret; 4 int pos[4]; 5 public: 6 bool check(string &s, int beg, int end) 7 { 8 string ip(s, beg, end - beg + 1); 9 if (ip.size() == 1)10 return "0" restoreIpAddresses(string s) {47 // Start typing your C/C++ s... 阅读全文

posted @ 2013-09-03 14:39 jumping_grass 阅读(191) 评论(0) 推荐(0)

unique binary search tree

摘要: 1 class Solution { 2 public: 3 vector generate(int left,int right ) 4 { 5 vector vec; 6 7 if( left > right ) 8 vec.push_back( NULL ); 9 10 for(int i=left;i vec1 = generate(left,i-1);13 vector vec2 = generate(i+1,right);14 ... 阅读全文

posted @ 2013-09-03 13:17 jumping_grass 阅读(179) 评论(0) 推荐(0)

导航