2013年5月26日

摘要: Problem:Analysis:Naive solution which compute every possible subarray sum and find the maximum need O(n^3), which is unbearable.A O(n) solutoin can be found based on the fact that. If the current sum is greater than or equal to 0, we keep add new element to it. If the current sum is less than 0, we 阅读全文
posted @ 2013-05-26 07:26 freeneng 阅读(150) 评论(0) 推荐(0) 编辑
摘要: Problem:Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.Analysis:anagrams are those strings that consist of same characters but in different order. A very simple way to solve the problem is try to find each string's anagrams. This 阅读全文
posted @ 2013-05-26 06:56 freeneng 阅读(135) 评论(0) 推荐(0) 编辑
摘要: Problem:Given a collection of numbers, return all possible permutations.For example,[1,2,3]have the following permutations:[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2], and[3,2,1].Analysis:It's a backtracking problem. In order to make sure that we don't access the same element multiple times, we 阅读全文
posted @ 2013-05-26 05:58 freeneng 阅读(113) 评论(0) 推荐(0) 编辑
摘要: Problem:Given a set of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Thesamerepeated number may be chosen fromCunlimited number of times.Note:All numbers (including target) will be positive integers.Elements in a combination (a1,a 阅读全文
posted @ 2013-05-26 05:01 freeneng 阅读(173) 评论(0) 推荐(0) 编辑
摘要: Problem: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 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 transact 阅读全文
posted @ 2013-05-26 02:27 freeneng 阅读(122) 评论(0) 推荐(0) 编辑
摘要: Problem:Mergeksorted linked lists and return it as one sorted list. Analyze and describe its complexity.Analysis:Since each list is sorted, the second element won't be merged into the final list until the first element is merged. So we can scan the head of each list and find the minimum element 阅读全文
posted @ 2013-05-26 02:09 freeneng 阅读(116) 评论(0) 推荐(0) 编辑

2013年5月25日

摘要: Problem:Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, givenn= 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"Analysis:Also a backtracking problem. Each time eit 阅读全文
posted @ 2013-05-25 14:33 freeneng 阅读(202) 评论(0) 推荐(0) 编辑
摘要: Problem:Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit string "23"Output: ["ad", "ae", "af", "bd", "be&q 阅读全文
posted @ 2013-05-25 13:08 freeneng 阅读(137) 评论(0) 推荐(0) 编辑
摘要: Problem:Given an arraySofnintegers, are there elementsa,b,c, anddinSsuch thata+b+c+d= target? Find all unique quadruplets in the array which gives the sum of target.Note:Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie,a?b?c?d)The solution set must not contain duplicate quadru 阅读全文
posted @ 2013-05-25 10:42 freeneng 阅读(150) 评论(0) 推荐(0) 编辑
摘要: Problem:Given an arraySofnintegers, find three integers inSsuch that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2 1 -4}, and target = 1. The sum that is clo... 阅读全文
posted @ 2013-05-25 10:27 freeneng 阅读(152) 评论(0) 推荐(0) 编辑

导航