摘要: Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transactions as you like(ie, buy one and sell one share of the stock multiple times).However, you may not engage in multiple transactions at 阅读全文
posted @ 2014-04-04 23:22 beehard 阅读(120) 评论(0) 推荐(0) 编辑
摘要: Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a solution is: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 1 c... 阅读全文
posted @ 2014-04-04 23:01 beehard 阅读(124) 评论(0) 推荐(0) 编辑
摘要: Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,If S = [1,2,3], a solution is:[[3],[1],[2],[1,2,3],[1,3],[2,3],[1,2],[]]Solution: 1. Recursive solution.2. Iterative solution. Contributed by yinlinglin. 1 class Solution { 2 public: 阅读全文
posted @ 2014-04-04 22:59 beehard 阅读(160) 评论(0) 推荐(0) 编辑
摘要: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[[2,4],[3,4],[2,3],[1,2],[1,3],[1,4],] 1 class Solution { 2 public: 3 vector > combine(int n, int k) { 4 vector > res; 5 vector com; 6 co... 阅读全文
posted @ 2014-04-04 22:58 beehard 阅读(113) 评论(0) 推荐(0) 编辑
摘要: The set [1,2,3,...,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3):"123""132""213""231""312""321"Given n and k, return the kth permutation 阅读全文
posted @ 2014-04-04 22:56 beehard 阅读(141) 评论(0) 推荐(0) 编辑
摘要: Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).The replacement must be in-place, do not allocate extra memory.He 阅读全文
posted @ 2014-04-04 22:53 beehard 阅读(131) 评论(0) 推荐(0) 编辑
摘要: Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.Solution: Sort the string to see if they're anagrams. 1 class Solution { 2 public: 3 vector anagrams(vector &strs) { 4 map > hashmap; // apply hashmap (BST) 5 for(int ... 阅读全文
posted @ 2014-04-04 22:49 beehard 阅读(120) 评论(0) 推荐(0) 编辑
摘要: Given a collection of candidate numbers (C) and a target number (T), find all unique combinationsin C where the candidate numbers sums to T.Each number in C may only be used once in the combination.Note:All numbers (including target) will be positive integers.Elements in a combination (a1, a2, .. , 阅读全文
posted @ 2014-04-04 22:45 beehard 阅读(140) 评论(0) 推荐(0) 编辑
摘要: Given a set of candidate numbers (C) and a target number (T), find all uniquecombinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited number of times.Note:All numbers (including target) will be positive integers.Elements in a combination (a1, a2 阅读全文
posted @ 2014-04-04 22:34 beehard 阅读(175) 评论(0) 推荐(0) 编辑
摘要: Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1]. 1 class Solution { 2 public: 3 vector > res; 4 5 void permuteUnique(vector &num, vector &per, v... 阅读全文
posted @ 2014-04-04 22:32 beehard 阅读(127) 评论(0) 推荐(0) 编辑