摘要: 参考:http://fisherlei.blogspot.com/2012/12/leetcode-median-of-two-sorted-arrays.html[解题思路]O(n)的解法比较直观,直接merge两个数组,然后求中间值。而对于O(log(m+n))显然是用二分搜索了, 相当于“Kth element in 2 sorted array”的变形。如果(m+n)为奇数,那么找到“(m+n)/2+1 th element in 2 sorted array”即可。如果(m+n)为偶数,需要找到(m+n)/2 th 及(m+n)/2+1 th,然后求平均。而对于“Kth elemen 阅读全文
posted @ 2014-01-25 17:40 Razer.Lu 阅读(469) 评论(0) 推荐(0) 编辑
摘要: Returns a pointer to the first occurrence of needle in haystack, ornullif needle is not part of haystack\遍历算法 1 public class Solution { 2 public String strStr(String haystack, String needle) { 3 4 if(haystack == null || needle == null) 5 return null; 6 if(needl... 阅读全文
posted @ 2014-01-25 17:31 Razer.Lu 阅读(189) 评论(0) 推荐(0) 编辑
摘要: Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.public class Solution { public int removeElement(int[] A, int elem) { int len = A.length; in... 阅读全文
posted @ 2014-01-25 17:29 Razer.Lu 阅读(103) 评论(0) 推荐(0) 编辑
摘要: Two pointerspublic class Solution { public ArrayList> threeSum(int[] num) { ArrayList> result = new ArrayList>(); Arrays.sort(num); ... 阅读全文
posted @ 2014-01-25 17:25 Razer.Lu 阅读(167) 评论(0) 推荐(0) 编辑
摘要: public class Solution { public int[] twoSum(int[] numbers, int target) { // IMPORTANT: Please reset any member data you declared, as ... 阅读全文
posted @ 2014-01-25 17:21 Razer.Lu 阅读(126) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ ... 阅读全文
posted @ 2014-01-25 14:18 Razer.Lu 阅读(130) 评论(0) 推荐(0) 编辑
摘要: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { if(l1 == null) return l2;... 阅读全文
posted @ 2014-01-25 14:03 Razer.Lu 阅读(119) 评论(0) 推荐(0) 编辑
摘要: 递归解法/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public boolean isValidBST(TreeNode root) { return isValidBSTHelper(root, Integer.MIN_VALUE, Integer.MAX_... 阅读全文
posted @ 2014-01-25 13:51 Razer.Lu 阅读(171) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofeverynode never differ by more than 1./** * Definition for binary tree * public class TreeNode { * int val; * TreeNode ... 阅读全文
posted @ 2014-01-25 06:30 Razer.Lu 阅读(141) 评论(0) 推荐(0) 编辑
摘要: Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B aremandnrespectively.public class Solution { public void merge(int A[], int m, int B[], int n) ... 阅读全文
posted @ 2014-01-25 06:16 Razer.Lu 阅读(178) 评论(0) 推荐(0) 编辑
摘要: 参考:http://www.cnblogs.com/reynold-lei/p/3385290.html http://www.cnblogs.com/feiling/p/3302242.html罗马数字共有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字。在较大的罗马数字的左边记上较小的罗马数字,表示大数字减小数字。左减的数字有限制,仅限于I、X、C。比如45不可以写成VL,只能是XLV但是,左减时不可跨越一个位数。比如,99不可以用IC()表示,而是用XCIX()表示。(等同于阿拉. 阅读全文
posted @ 2014-01-25 05:51 Razer.Lu 阅读(215) 评论(0) 推荐(0) 编辑
摘要: 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].public cl 阅读全文
posted @ 2014-01-25 05:11 Razer.Lu 阅读(138) 评论(0) 推荐(0) 编辑
摘要: 参考 http://fisherlei.blogspot.com/2012/12/leetcode-largest-rectangle-in-histogram.html以下摘自水中的鱼:这样的话,就可以通过大数据。但是这个优化只是比较有效的剪枝,算法仍然是O(n*n).想了半天,也想不出来O(n)的解法,于是上网google了一下。如下图所示,从左到右处理直方,i=4时,小于当前栈顶(及直方3),于是在统计完区间[2,3]的最大值以后,消除掉阴影部分,然后把红线部分作为一个大直方插入。因为,无论后面还是前面的直方,都不可能得到比目前栈顶元素更高的高度了。这就意味着,可以维护一个递增的栈,每次 阅读全文
posted @ 2014-01-25 03:35 Razer.Lu 阅读(306) 评论(0) 推荐(0) 编辑
摘要: Trapping Rain WaterGiven n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example,Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In 阅读全文
posted @ 2014-01-25 02:20 Razer.Lu 阅读(234) 评论(0) 推荐(0) 编辑