随笔分类 -  leetcode

Swap Nodes in Pairs
摘要:1 ListNode *swapPairs(ListNode *head) { 2 if(head==NULL||head->next==NULL)//链表为空或者只有一个节点 3 return head; 4 ListNode *p,*q,*h; 5 p=head; 6 q=head->next; 7 p->next=q->next; 8 q->next=p; 9 head=q;//头处理完了,其实这个时候就和一般情况下交换过之后是一样的了,就可以... 阅读全文

posted @ 2014-03-26 22:27 crane_practice 阅读(111) 评论(0) 推荐(0)

Maximum Subarray
摘要:1 /* 2 从头开始,每次加过之后和之前的最大值比较,若比最大值大,则更新最大值 3 如果加到某个数时,值为负,那么下次累加的起点就要改变 4 */ 5 int maxSubArray(int A[], int n) { 6 if(A==NULL||nmax?sum:max;12 if(sum<0)13 sum=0;//如果加到某个数时,值为负,那么下次累加的起点就要改变14 }15 return max;16 }AC 阅读全文

posted @ 2014-03-25 20:22 crane_practice 阅读(99) 评论(0) 推荐(0)

Unique Paths
摘要:1 int uniquePaths(int m, int n) { 2 if(m==0||n==0) 3 return 0; 4 int A[m][n]; 5 int i,j; 6 for(i=0;i<m;++i) 7 A[i][0]=1; 8 for(i=1;i<n;++i) 9 A[0][i]=1;10 for(i=1;i<m;++i){11 for(j=1;j<n;++j)12 ... 阅读全文

posted @ 2014-03-25 16:48 crane_practice 阅读(149) 评论(0) 推荐(0)

Merge Two Sorted Lists
摘要:1 ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { 2 if(l1==NULL) 3 return l2; 4 if(l2==NULL) 5 return l1; 6 ListNode *head,*p; 7 if(l1->valval){ 8 head=l1; 9 l1=l1->next;10 }11 else{12 ... 阅读全文

posted @ 2014-03-25 11:45 crane_practice 阅读(99) 评论(0) 推荐(0)

Climbing Stairs
摘要:1 int climbStairs(int n) 2 { 3 if(n<=0) 4 return 0; 5 if(n==1) 6 return 1; 7 if(n==2) 8 return 2; 9 return climbStairs(n-1)+climbStairs(n-2);10 }青蛙跳台阶问题,第一反应就是上面的代码了,结果是 超时 1 int climbStairs(int n) { 2 if(n<=0) 3 return 0; 4 in... 阅读全文

posted @ 2014-03-25 11:06 crane_practice 阅读(124) 评论(0) 推荐(0)

Sort Colors
摘要:其实这个Follow up说的也很好哇,计数排序A rather straight forward solution is a two-pass algorithm using counting sort.First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's. 1 void sortColors(int A[], int n) 阅读全文

posted @ 2014-03-24 19:49 crane_practice 阅读(184) 评论(0) 推荐(0)

Merge Sorted Array
摘要:如果另外开辟一个数组C,A,B从头开始比较,谁小,谁最先放到中,题上说最后都放到A中,那么就扫描C,复制数据就行了,时间复杂度O(m+n),空间复杂度O(m+n)当然还有比这个好的办法,空间复杂度O(1). 1 void merge(int A[], int m, int B[], int n) { 2 int C[m+n]; 3 int i=0,j=0,k=0; 4 while(i=0&&j>=0){ 7 if(A[i]=0){18 A[k]=A[i];19 k--... 阅读全文

posted @ 2014-03-23 19:17 crane_practice 阅读(135) 评论(0) 推荐(0)

Gray Code
摘要:1 //reference http://blog.csdn.net/fightforyourdream/article/details/14517973 2 vector grayCode(int n) { 3 vector codes; 4 if(n>1;11 codes.push_back(tmp);12 }13 return codes;14 } 阅读全文

posted @ 2014-03-22 17:35 crane_practice 阅读(138) 评论(0) 推荐(0)

Populating Next Right Pointers in Each Node II ?
摘要:1 void connect(TreeLinkNode *root) { 2 if(root==NULL) 3 return; 4 if(root->left&&root->right) 5 root->left->next=root->right; 6 if(root->next){ 7 if(root->left!=NULL&&root->right==NULL){ 8 if(root->next->left) 9 ... 阅读全文

posted @ 2014-03-22 11:59 crane_practice 阅读(165) 评论(0) 推荐(0)

Populating Next Right Pointers in Each Node
摘要:1 //按说层次遍历最方便了,但是空间复杂度不满足题意 2 //遍历吧,可是像例子中的,5—>6怎么实现呢 3 //You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children). 4 //这个提示是什么意思? 5 void connect(TreeLinkNode *root) { 6 if(root==NULL) 7 return;... 阅读全文

posted @ 2014-03-22 10:51 crane_practice 阅读(205) 评论(0) 推荐(0)

Pascal's Triangle
摘要:1 vector > generate(int numRows) { 2 int i,j; 3 vector > intVV; 4 vector intV; 5 if(numRows >' 7 return intVV; 8 intV[0].push_back(1); 9 intVV[0].push_back(intV[0]);10 for(i=1;i > generate(int numRows) { 2 int i,j; 3 ... 阅读全文

posted @ 2014-03-21 23:01 crane_practice 阅读(208) 评论(0) 推荐(0)

Best Time to Buy and Sell Stock
摘要:先说点股票的事,对股票一无所知,我问明哥,比如说我今天10元买了个股票,明天11元,后天12元,那我后天是不是赚了3元?他说不是,股票只有卖了才有钱,也就是说后天卖了的话,赚2元。一般都会提到的算法是分治和动态规划(《剑指offer》中的第一种方法也很好,容易理解),动态规划后面再去实现,看到http://blog.csdn.net/iammiaoyan/article/details/11730059中说的另外一种感觉容易被忽视的方法(虽然很常见):要求最大的收益,自然是用最高的股票价格减去最低的价格即可,考虑到时间的因素,当前的收益一定是当天价格减去前面几天里的最低价格即可。 1 ... 阅读全文

posted @ 2014-03-21 22:02 crane_practice 阅读(198) 评论(0) 推荐(0)

Single Number II
摘要:转自:http://blog.csdn.net/kenden23/article/details/136252971 int singleNumber(int A[], int n) { 2 for(int i = 0; i 3<2不成立,然后呢,就木有然后了 1 int singleNumber(int A[], int n) { 2 if(A==NULL||n<=0) 3 return 0; 4 //if(n<=2) 5 //return A[0]; 6 sort... 阅读全文

posted @ 2014-03-19 17:30 crane_practice 阅读(147) 评论(0) 推荐(0)

Single Number
摘要:1 int singleNumber(int A[], int n) {2 if(A==NULL||n<=0)//这是个好习惯,防止程序崩溃3 return 0;4 int i;5 int result=A[0];6 for(i=1;i<n;++i)7 result ^= A[i];8 return result;9 } 阅读全文

posted @ 2014-03-19 15:23 crane_practice 阅读(97) 评论(0) 推荐(0)

Linked List Cycle II
摘要:1 ListNode *detectCycle(ListNode *head) { 2 if(!head||!head->next) 3 return NULL; 4 ListNode *fast,*slow; 5 fast=head; 6 slow=head; 7 while(fast&&fast->next) 8 { 9 slow=slow->next;10 fast=fast->next->next;11 ... 阅读全文

posted @ 2014-03-18 21:20 crane_practice 阅读(118) 评论(0) 推荐(0)

Linked List Cycle
摘要:1 bool hasCycle(ListNode *head) { 2 if(head==NULL) 3 return false; 4 ListNode *pBefore,*pLater; 5 if(head->next) 6 pBefore=head->next; 7 else 8 return false; 9 pLater=head;10 while(pBefore)11 {12 ... 阅读全文

posted @ 2014-03-18 17:29 crane_practice 阅读(539) 评论(0) 推荐(0)

Palindrome Number
摘要:1 bool isPalindrome(int x) { 2 if(x=10) 9 div*=10;10 while(x)11 {12 i=x/div;13 j=x%10;14 if(i!=j)15 return false;16 x=(x%div)/10;17 div /= 100;18 }19 return true;20 ... 阅读全文

posted @ 2014-03-12 21:09 crane_practice 阅读(178) 评论(0) 推荐(0)

String to Integer (atoi) ???
摘要:1 #define INT_MAX 2147483647 2 #define INT_MIN -2147483648 3 class Solution { 4 public: 5 int atoi(const char *str) { 6 //string如果能转换成int的话,各位都要是数字,一个例外是第一个字符可以是负号 7 int n=strlen(str); 8 int i=0; 9 int flag=0;//0表示正值,1表示负值10 int val=0;11 /*12 ... 阅读全文

posted @ 2014-03-12 09:27 crane_practice 阅读(419) 评论(0) 推荐(0)

Reverse Integer
摘要:1 int reverse(int x) {2 int y=0;3 while(x)4 {5 y=y*10+x%10;6 x=x/10;7 }8 return y;9 }我试过了,不用区分是正数还是负数,他们的计算方法是一样的上面的代码虽然AC了,不过题上spoilers说的好,有可能会溢出,这个上述代码并没有考虑32位,1位符号位,剩下31位,表示范围 From−2,147,483,648 to 2,147,483,647, from −(231) ... 阅读全文

posted @ 2014-03-11 15:44 crane_practice 阅读(183) 评论(0) 推荐(0)

Two Sum
摘要:之前遇到一道题和这个基本一样,数组中元素各不相同,另外创建一个数组,里面的元素为target依次减去原数组中所有元素,然后两个数组合并,排序,扫描,如果有相邻元素相等并且不等于target的一半,则说明原数组中有两个不同的元素和为target。不过这个题目的返回值是两个,这个怎么做到?而且函数的返回值类型为vector······这个我现在真不会,参看了别人的写法才知道是这么写的:vectorret(2,0)Submission Result:Runtime Error,不要怕错,要从错误中吸取经验和教训 1 class Soluti 阅读全文

posted @ 2014-03-11 14:42 crane_practice 阅读(233) 评论(0) 推荐(0)

导航