随笔分类 -  Algorithm

摘要: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
摘要: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
摘要: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
摘要: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
摘要:题目:输入一个整型数组,数据元素有正数也有负数,求元素组合成连续子数组之和最大的子数组,要求时间复杂度为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