上一页 1 ··· 6 7 8 9 10 11 12 13 14 ··· 20 下一页
摘要: void swap(int* a,int* b){ int tmp = *a; *a = *b; *b = tmp;}class Solution {public: int removeElement(int A[], int n, int elem) { // Note: The Solution object is instantiated only once and is reused by each test case. int i = 0; int j = n; while(i<j) { ... 阅读全文
posted @ 2013-10-04 22:53 summer_zhou 阅读(144) 评论(0) 推荐(0) 编辑
摘要: int threeSumClosest(vector &num, int target) { // Note: The Solution object is instantiated only once and is reused by each test case. sort(num.begin(),num.end()); int i,j,k; int sum,cursum; int curDiff = INT_MAX; for(i=0;itarget) k--;... 阅读全文
posted @ 2013-10-04 22:12 summer_zhou 阅读(124) 评论(0) 推荐(0) 编辑
摘要: int atoi(const char *str) { // Note: The Solution object is instantiated only once and is reused by each test case. //skip whitespace const char* p = str; while(*p==' ') ++p; if(*p!='+'&&*p!='-'&&!isdigit(*p)) return 0; bool bNeg... 阅读全文
posted @ 2013-10-04 21:52 summer_zhou 阅读(145) 评论(0) 推荐(0) 编辑
摘要: O(n)的解法。详情见:http://www.felix021.com/blog/read.php?2040 string longestPalindrome(string s) { // Note: The Solution object is instantiated only once and is reused by each test case. if(s.empty()) return ""; int mx,i,id; //construct p.Transform S into T.For exa... 阅读全文
posted @ 2013-10-04 21:36 summer_zhou 阅读(142) 评论(0) 推荐(0) 编辑
摘要: findKthTwoSortedArray的应用。double findMedianSortedArrays(int A[], int m, int B[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function if(((m+n)&0x1)!=0) //odd { return findKthSortedArrays(A,m,B,n,(m+n+1)>>1); }else ... 阅读全文
posted @ 2013-10-04 12:02 summer_zhou 阅读(128) 评论(0) 推荐(0) 编辑
摘要: bool isValidBST(TreeNode *root) { // Note: The Solution object is instantiated only once and is reused by each test case. return helper(root,INT_MIN,INT_MAX); } bool helper(TreeNode* root,int lower,int upper) { if(!root) return true; if... 阅读全文
posted @ 2013-10-03 23:26 summer_zhou 阅读(128) 评论(0) 推荐(0) 编辑
摘要: Two pointers问题。留心一个问题,num.size()返回的是无符号数。当其值为1,i==0时,i > threeSum(vector &num) { // Start typing your C/C++ solution below // DO NOT write int main() function vector> res; if(num.empty()) return res; sort(num.begin(),num.end()); int i,j,k; ... 阅读全文
posted @ 2013-10-03 23:12 summer_zhou 阅读(137) 评论(0) 推荐(0) 编辑
摘要: A: 二分Maintaining the invarianti+j=k– 1,If Bj-1= 0); assert(n >= 0); assert(k > 0); assert(k = 0); assert(j >= 0); assert(i Bj && Ai_1 > Bj) || (Ai < Bj && Ai < Bj_1)); // if none of the cases above, then it is either: if (Ai < Bj) // exclude Ai and below portion 阅读全文
posted @ 2013-10-03 19:04 summer_zhou 阅读(474) 评论(0) 推荐(0) 编辑
摘要: int lengthOfLongestSubstring(string s) { // Note: The Solution object is instantiated only once and is reused by each test case. int max_len = 0,cur_len = 0; int hash[256]; for(int i=0;i<256;i++) hash[i] = -1; for(int i=0;i<s.size();i... 阅读全文
posted @ 2013-10-03 10:28 summer_zhou 阅读(132) 评论(0) 推荐(0) 编辑
摘要: Q: 求出数组中只出现1次的数,其他数都出现2次。 int singleNumber(int A[], int n) { // Note: The Solution object is instantiated only once and is reused by each test case. int num = 0; for(int i=0;i<n;i++) num^=A[i]; return num; } 阅读全文
posted @ 2013-10-02 22:51 summer_zhou 阅读(117) 评论(0) 推荐(0) 编辑
上一页 1 ··· 6 7 8 9 10 11 12 13 14 ··· 20 下一页