上一页 1 ··· 4 5 6 7 8
摘要: Code:O(n):class Solution {public: vector twoSum(vector &numbers, int target) { vector res; map mapping; int n=numbers.size(); for (int i=0; i twoSum(vector &numbers, int target) { vector res; int n=numbers.size(); for(int i=0;i<n;i++) fo... 阅读全文
posted @ 2013-10-28 00:24 WinsCoder 阅读(101) 评论(0) 推荐(0) 编辑
摘要: Code:class Solution {public: int maxDep; vector buf; // pack numbers into an array vector> res; // pack arrays into an result void dfs(int dep, vector &valid, vector &num){ if(dep==maxDep){ res.push_back(buf); return; } for(int i=0; ... 阅读全文
posted @ 2013-10-26 15:57 WinsCoder 阅读(118) 评论(0) 推荐(0) 编辑
摘要: Code:class Solution {public: int singleNumber(int A[], int n) { // Note: The Solution object is instantiated only once and is reused by each test case. int res = 0; int bit,i,j; for(i=0;i>i)&1==1) bit = (bit+1)%3; } if(bit!=... 阅读全文
posted @ 2013-10-23 02:40 WinsCoder 阅读(93) 评论(0) 推荐(0) 编辑
摘要: Analysis:1. Sort the vector;2. Set i, j, k and traverse;3. Start from i, j is after i, and k is the last element;4. If theTrick: By increasing j and decreasing k, to reduce the complexity from n^3 to n^2.class Solution {public: int threeSumClosest(vector &num, int target) { sort(num.begin(... 阅读全文
posted @ 2013-10-21 10:51 WinsCoder 阅读(127) 评论(0) 推荐(0) 编辑
摘要: Definition for singly-linked list./** * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */Method 1: (Double-Pointer)class Solution {public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { ListNode *p1 = NULL; ListNode *... 阅读全文
posted @ 2013-10-19 07:29 WinsCoder 阅读(119) 评论(0) 推荐(0) 编辑
摘要: Objective:Find out the lowest point to buy-in and find out the a following hightest point to sell-out. And the maximal profit is equivalent to the max difference between the highest point and lowest point.Before solving this problem, suggest referring to the "maximum subarray" first. Then 阅读全文
posted @ 2013-10-17 02:33 WinsCoder 阅读(160) 评论(0) 推荐(0) 编辑
摘要: Code:class Solution {public: int maxDep; vectorbuf; vector> res; void dfs(int dep, vector &valid, vector &num){ if(dep==maxDep){ res.push_back(buf); return; } for(int i=0;i> permuteUnique(vector &num) { buf.clear(); res.clear(); ... 阅读全文
posted @ 2013-10-17 02:03 WinsCoder 阅读(153) 评论(0) 推荐(0) 编辑
摘要: 题目:输入一个整型数组,数据元素有正数也有负数,求元素组合成连续子数组之和最大的子数组,要求时间复杂度为O(n)。例如:输入的数组为1, -2,3, 10, -4, 7, 2, -5,最大和的连续子数组为3, 10, -4, 7, 2,其最大和为18。背景:本题最初为2005年浙江大学计算机系考研题的最后一道程序设计题,在2006年里包括google在内的很多知名公司都把本题当作面试题。由于本题在网络中广为流传,本题也顺利成为2006年程序员面试题中经典中的经典。分析:如果不考虑时间复杂度,我们可以枚举出所有子数组并求出他们的和。不过非常遗憾的是,由于长度为n的数组有O(n2)个子数组(即:n 阅读全文
posted @ 2013-10-17 00:58 WinsCoder 阅读(257) 评论(0) 推荐(0) 编辑
上一页 1 ··· 4 5 6 7 8