上一页 1 ··· 7 8 9 10 11 12 13 14 15 ··· 17 下一页

2013年11月3日

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 @ 2013-11-03 21:15 waruzhi 阅读(188) 评论(0) 推荐(0) 编辑

Edit Distance

摘要: Given two wordsword1andword2, find the minimum number of steps required to convertword1toword2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:a) Insert a characterb) Delete a characterc) Replace a character思路:动规代码: 1 int min(int a, int b){ 2 ... 阅读全文

posted @ 2013-11-03 20:52 waruzhi 阅读(150) 评论(0) 推荐(0) 编辑

Linked List Cycle II

摘要: Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up:Can you solve it without using extra space?思路:在上一题的基础上,假设head到环入口的距离为a,相遇点到环入口的距离为b,换的周长为r,则第一次相遇时有2*(a+b) = a + b + nra = nr - b也就是说如果从相遇点和head处同时出发两个指针,步数一样,那么就会在环入口相遇,据此可以获得入口的指针代码 ListNode ... 阅读全文

posted @ 2013-11-03 10:05 waruzhi 阅读(153) 评论(0) 推荐(0) 编辑

Linked List Cycle

摘要: Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?思路:快慢指针。两个指针同时从head出发,一个每次加一,一个每次加二,如果相遇的话就存在环。代码 1 bool hasCycle(ListNode *head) { 2 // IMPORTANT: Please reset any member data you declared, as 3 // the same Solution ins... 阅读全文

posted @ 2013-11-03 10:02 waruzhi 阅读(112) 评论(0) 推荐(0) 编辑

Next Permutation

摘要: 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 @ 2013-11-03 09:07 waruzhi 阅读(251) 评论(0) 推荐(0) 编辑

2013年10月25日

Search for a Range

摘要: Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order ofO(logn).If the target is not found in the array, return[-1, -1].For example,Given[5, 7, 7, 8, 8, 10]and target value 8,return[3, 4].思路二分查找的变形 阅读全文

posted @ 2013-10-25 19:13 waruzhi 阅读(251) 评论(0) 推荐(0) 编辑

Set Matrix Zeroes

摘要: Given amxnmatrix, if an element is 0, set its entire row and column to 0. Do it in place.Follow up:Did you use extra space?A straight forward solution using O(mn) space is probably a bad idea.A simple improvement uses O(m+n) space, but still not the best solution.Could you devise a constant space so 阅读全文

posted @ 2013-10-25 18:44 waruzhi 阅读(240) 评论(0) 推荐(0) 编辑

2013年10月18日

Length of Last Word

摘要: Given a stringsconsists of upper/lower-case alphabets and empty space characters' ', return the length of last word in the string.If the last word does not exist, return 0.Note:A word is defined as a character sequence consists of non-space characters only.For example,Givens="Hello Worl 阅读全文

posted @ 2013-10-18 20:17 waruzhi 阅读(144) 评论(0) 推荐(0) 编辑

Interleaving String

摘要: Givens1,s2,s3, find whethers3is formed by the interleaving ofs1ands2.For example,Given:s1="aabcc",s2="dbbca",Whens3="aadbbcbcac", return true.Whens3="... 阅读全文

posted @ 2013-10-18 20:06 waruzhi 阅读(205) 评论(0) 推荐(0) 编辑

Sum Root to Leaf Numbers

摘要: Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.An example is the root-to-leaf path1->2->3which represents the number123.Find the total sum of all root-to-leaf numbers.For example, 1 / \ 2 3The root-to-leaf path1->2represents the number12.T 阅读全文

posted @ 2013-10-18 18:58 waruzhi 阅读(190) 评论(0) 推荐(0) 编辑

上一页 1 ··· 7 8 9 10 11 12 13 14 15 ··· 17 下一页

导航