2014年3月11日

Jump Game

摘要: Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you are able to reach the last index.For example:A =[2,3,1,1,4], returntrue.A =[3,2,1,0,4], returnfalse.最直 阅读全文

posted @ 2014-03-11 23:11 pengyu2003 阅读(132) 评论(0) 推荐(0)

Maximum Subarray

摘要: Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array[−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray[4,−1,2,1]has the largest sum =6.click to show more practice.More practice:If you have figured out the O(n) solution, try 阅读全文

posted @ 2014-03-11 22:56 pengyu2003 阅读(158) 评论(0) 推荐(0)

Pow(x, n)

摘要: Implement pow(x,n).很有趣的题,第一次暴力乘了,超时。然后就想到多项式分解了,想到二进制,想到了取次数相当于二进制遇“0”不管,遇“1”乘x,移位取平方。代码测试后出现负次数没有过,加了这部分代码就过了。代码不难,优化只需要小动脑,还是挺好玩的。class Solution {public: double pow(double x, int n) { double result =1; int flag = 0; if(n sk; while(n>0) { sk.push(n%2... 阅读全文

posted @ 2014-03-11 22:13 pengyu2003 阅读(123) 评论(0) 推荐(0)

Anagrams

摘要: Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.题意是,回文如 eat ate tea都是回文。明白题意就很好办了。map里的元素两种:value为负说明已经入vector。若为正,表示第一次出现,具体位置是value。再次相遇则value改为负且最初位置和当前位置串入vectorclass Solution {public: vector anagrams(vector &strs) { vect... 阅读全文

posted @ 2014-03-11 21:31 pengyu2003 阅读(110) 评论(0) 推荐(0)

Permutations

摘要: 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].简单递归题,同样没查最佳算法,今天还没看书呢……class Solution {public: vector > re; vector > permute(vector &num) { vector no; vector co... 阅读全文

posted @ 2014-03-11 20:39 pengyu2003 阅读(130) 评论(0) 推荐(0)

Combination Sum

摘要: 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,a2, … ,ak 阅读全文

posted @ 2014-03-11 19:39 pengyu2003 阅读(114) 评论(0) 推荐(0)

导航