10 2013 档案
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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...
阅读全文
摘要: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(...
阅读全文
摘要: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...
阅读全文
摘要: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&
阅读全文
摘要: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 ...
阅读全文
摘要: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...
阅读全文
摘要: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; ...
阅读全文
摘要: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!=...
阅读全文
摘要: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(...
阅读全文
摘要: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 *...
阅读全文
摘要: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
阅读全文
摘要: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(); ...
阅读全文
摘要:题目:输入一个整型数组,数据元素有正数也有负数,求元素组合成连续子数组之和最大的子数组,要求时间复杂度为O(n)。例如:输入的数组为1, -2,3, 10, -4, 7, 2, -5,最大和的连续子数组为3, 10, -4, 7, 2,其最大和为18。背景:本题最初为2005年浙江大学计算机系考研题的最后一道程序设计题,在2006年里包括google在内的很多知名公司都把本题当作面试题。由于本题在网络中广为流传,本题也顺利成为2006年程序员面试题中经典中的经典。分析:如果不考虑时间复杂度,我们可以枚举出所有子数组并求出他们的和。不过非常遗憾的是,由于长度为n的数组有O(n2)个子数组(即:n
阅读全文