摘要: public class Solution { public int divide(int dividend, int divisor) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if(dividend == 0) return 0; if(divisor == 1) return... 阅读全文
posted @ 2014-01-26 16:48 Razer.Lu 阅读(133) 评论(0) 推荐(0) 编辑
摘要: Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possibl... 阅读全文
posted @ 2014-01-26 08:02 Razer.Lu 阅读(152) 评论(0) 推荐(0) 编辑
摘要: public class Solution { public boolean isPalindrome(String s) { if(s == null || s.length() <2 ){ return true; } String mystring = s.replaceAll("[^A-Za-z0-9]", ""); mystring = mystring.toLowerCase(); // 只需遍历前一半就可以了 for(int i = 0... 阅读全文
posted @ 2014-01-26 07:18 Razer.Lu 阅读(93) 评论(0) 推荐(0) 编辑
摘要: [解题思路]典型的递归。一步步构造字符串。当左括号出现次数 generateParenthesis(int n) { ArrayList result = new ArrayList(); StringBuilder builder = new StringBuilder(); generate(result, builder, n, n); return result; } public void generate(ArrayList result, StringBuilder builder, int start,... 阅读全文
posted @ 2014-01-26 07:07 Razer.Lu 阅读(119) 评论(0) 推荐(0) 编辑
摘要: 123 * 45 615 + 492 = 5535public class Solution { public String multiply(String num1, String num2) { int l1 = num1.l... 阅读全文
posted @ 2014-01-26 04:08 Razer.Lu 阅读(125) 评论(0) 推荐(0) 编辑
摘要: 有三种情况要考虑:1. 如果两个substring相等的话,则为true2. 如果两个substring中间某一个点,左边的substrings为scramble string,同时右边的substrings也为scramble string,则为true3. 如果两个substring中间某一个点,s1左边的substring和s2右边的substring为scramblestring, 同时s1右边substring和s2左边的substring也为scramblestring,则为truepublic class Solution { public boolean isScramb... 阅读全文
posted @ 2014-01-26 03:25 Razer.Lu 阅读(154) 评论(0) 推荐(0) 编辑
摘要: 参考: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) 编辑