上一页 1 ··· 3 4 5 6 7 8 下一页
摘要: Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up:Can you solve it without using extra space?Code:class Solution {public: ListNode *detectCycle(ListNode *head) { ListNode *p=head; ListNode *cur=head; while(p){ i... 阅读全文
posted @ 2013-11-01 05:45 WinsCoder 阅读(144) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, swap every two adjacent nodes and return its head.For example,Given1->2->3->4, you should return the list as2->1->4->3.Your algorithm should use only constant space. You maynotmodify the values inthe list, only nodesitself can bechanged.Code:class Solution {pub 阅读全文
posted @ 2013-11-01 05:35 WinsCoder 阅读(133) 评论(0) 推荐(0) 编辑
摘要: Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis at (i,ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.Note: You 阅读全文
posted @ 2013-10-31 08:46 WinsCoder 阅读(120) 评论(0) 推荐(0) 编辑
摘要: Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete at mosttwotransactions.Note:You may not engage in multiple transactions at the same time (ie, you must sell thestock before you buy again).Code:Method1 阅读全文
posted @ 2013-10-31 03:49 WinsCoder 阅读(149) 评论(0) 推荐(0) 编辑
摘要: Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete at mosttwotransactions.Note:You may not engage in multiple transactions at the same time (ie, you must sell thestock before you buy again).Code:class S 阅读全文
posted @ 2013-10-31 03:48 WinsCoder 阅读(115) 评论(0) 推荐(0) 编辑
摘要: Code:class Solution {public: bool hasCycle(ListNode *head) { ListNode *p=head; ListNode *cur=head; while(p){ if(p->next) p=p->next->next; else return false; cur=cur->next; if(p==cur) retu... 阅读全文
posted @ 2013-10-30 07:46 WinsCoder 阅读(101) 评论(0) 推荐(0) 编辑
摘要: Code:class Solution {public: vector > fourSum(vector &num, int target) { vector buf; vector> res; if(num.size()p+2;q--) { i=p+1; j=q-1; while(ip+2&&num[q]==num[q-1]) q--; } while(... 阅读全文
posted @ 2013-10-30 07:44 WinsCoder 阅读(165) 评论(0) 推荐(0) 编辑
摘要: Code:class Solution {public: vector> threeSum(vector &num) { vector buf; vector> res; if(num.empty()) return res; sort(num.begin(),num.end()); int n=num.size(); int i,j,k; for(i=0;i<n-2;i++) { j=i+1; k=n-1; w... 阅读全文
posted @ 2013-10-30 07:42 WinsCoder 阅读(133) 评论(0) 推荐(0) 编辑
摘要: Code:class Solution {public: string intToRoman(int num) { string roman; int nums[13] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; char symbols[13] = {'M', 'C', 'D', 'C', 'C', 'X', 'L', 'X', 'X', 'I', 'V& 阅读全文
posted @ 2013-10-30 07:41 WinsCoder 阅读(130) 评论(0) 推荐(0) 编辑
摘要: Code:class Solution {public: void merge(int A[], int m, int B[], int n) { int i, j, count; for(i=0,j=0;i=B[j];j++) // count how many smaller than i count++; if(count>0){ // if there are some elements from B that need to insert into A for(int ... 阅读全文
posted @ 2013-10-28 07:45 WinsCoder 阅读(143) 评论(0) 推荐(0) 编辑
上一页 1 ··· 3 4 5 6 7 8 下一页