上一页 1 ··· 3 4 5 6 7 8 9 10 11 下一页

2014年3月12日

Simplify Path

摘要: Given an absolute path for a file (Unix-style), simplify it.For example,path="/home/", =>"/home"path="/a/./b/../../c/", =>"/c"click to show corner cases.Corner Cases:Did you consider the case wherepath="/../"?In this case, you should return&quo 阅读全文

posted @ 2014-03-12 14:23 pengyu2003 阅读(124) 评论(0) 推荐(0)

Minimum Path Sum

摘要: Given amxngrid filled with non-negative numbers, find a path from top left to bottom right whichminimizesthe sum of all numbers along its path.Note:You can only move either down or right at any point in time.简单的动态规划题。刚开始又写成路径和了……class Solution {public: int minPathSum(vector > &grid) { vect... 阅读全文

posted @ 2014-03-12 10:48 pengyu2003 阅读(173) 评论(0) 推荐(0)

Unique Paths II

摘要: Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as1and0respectively in the grid.For example,There is one obstacle in the middle of a 3x3 grid as illustrated below.[ [0,0,0], [0,1, 阅读全文

posted @ 2014-03-12 10:39 pengyu2003 阅读(105) 评论(0) 推荐(0)

Rotate List

摘要: Given a list, rotate the list to the right bykplaces, wherekis non-negative.For example:Given1->2->3->4->5->NULLandk=2,return4->5->1->2->3->NULL.题意描述很模糊,经过多次失败的尝试,总算明白大概是啥意思了……/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * Li 阅读全文

posted @ 2014-03-12 09:53 pengyu2003 阅读(103) 评论(0) 推荐(0)

Spiral Matrix II

摘要: Given an integern, generate a square matrix filled with elements from 1 ton2in spiral order.For example,Givenn=3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]写一个回旋的矩阵class Solution {public: vector > generateMatrix(int n) { vector >re; for(int i = 0 ... 阅读全文

posted @ 2014-03-12 09:25 pengyu2003 阅读(142) 评论(0) 推荐(0)

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)

上一页 1 ··· 3 4 5 6 7 8 9 10 11 下一页

导航