摘要: bool isBalanced(TreeNode *root) { // Note: The Solution object is instantiated only once and is reused by each test case. int depth; return helper(root,depth); } bool helper(TreeNode* root,int& depth) { if(!root) { depth = 0; ... 阅读全文
posted @ 2013-10-04 23:00 summer_zhou 阅读(106) 评论(0) 推荐(0) 编辑
摘要: 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) 编辑