OJ_leetcode
violin2696
这个作者很懒,什么都没留下…
展开
-
3sum
一直没看清英文题目,导致各种WA!唉唉唉。。。 通过Judge Small后: [code="java"] public class Solution { public ArrayList threeSum(int[] num) { int length = num.length; ArrayList results = new ArrayLis...原创 2012-10-27 01:51:10 · 56 阅读 · 0 评论 -
Container With Most Water
本来以为是个简单的题目,直接二重循环,结果小测试过了,大测试超时了= = 1万个数据啊,必须优化了 [code="java"] public class Solution { public int maxArea(int[] height) { // Start typing your Java solution below // DO NOT w...原创 2012-11-04 00:25:37 · 78 阅读 · 0 评论 -
Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Sear
[code="java"] /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public cl...原创 2012-11-04 17:36:08 · 77 阅读 · 0 评论 -
Count and Say
[code="java"] public class Solution { public String countAndSay(int n) { // Start typing your Java solution below // DO NOT write main() function ArrayList current = n...原创 2012-11-04 18:46:59 · 75 阅读 · 0 评论 -
Distinct Subsequences
动规,从前到后用T的每一个字符i,扫描S的每一个字符j。维护一个数组的更新,表示到j位置的子串有多少子序列和到i之前字串匹配的。然后在递推的时候,只需要看i是否和j匹配,然后匹配就说明有新的j可以和之前的子串组成符合条件的串。 开始边界条件 [code="java"] public class Solution { public int numDistinct(String S, ...原创 2012-11-04 21:44:59 · 74 阅读 · 0 评论 -
Divide Two Integers
自己实现除法 太太太恶心了。。。。 就是用位移代替了乘法,然后不断减去位移得到的乘数*除数 主要是测试数据还有一个Java.MinValue的问题 = = 取了abs之后它居然还是负数。。。我日 [code="java"] public class Solution { public int divide(int dividend, int divisor) { ...原创 2012-11-05 00:12:51 · 77 阅读 · 0 评论 -
Edit Distance
动规 就是递推。。。比较难想 然后数组长度设置比字符串长度多一,并做一定的初始化,都是为了方便边界条件的设置。。。。 [code="java"] public class Solution { public int minDistance(String word1, String word2) { // Start typing your Java solution...原创 2012-11-06 00:27:39 · 103 阅读 · 0 评论 -
First Missing Positive
唉 想了很久都没想出来 后来还是看了网上的答案 >_< public class Solution { public int firstMissingPositive(int[] A) { // Start typing your Java solution below // DO NOT write main() function ...原创 2012-11-06 22:50:09 · 90 阅读 · 0 评论 -
Flatten Binary Tree to Linked List && Generate Parentheses && Gray Code
Flatten太简单了 递归 一遍过 oh yeah = = 也没什么好骄傲的 [code="java"] /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(...原创 2012-11-07 00:08:57 · 73 阅读 · 0 评论 -
Implement strStr()
唉 终于到了要记算法的时候了 KMP。。。还没写完 回去再写。。。 。。。 首先可以看这篇文章 http://www.programfan.com/blog/article.asp?id=15762 然后之前代码完全写错了,我觉得KMP的理解和记忆的核心在于两点:1、KMP中next数组的目的在于知道某个字母失配之后应该怎么根据模式串已匹配的字符找最近的最大匹配 2、next数组的生成过程...原创 2012-11-07 15:44:33 · 82 阅读 · 0 评论 -
Construct Binary Tree from Inorder and Postorder Traversal
不知道为什么错了。。。eclipse上明明是正确的啊 leetcode上就显示runtime error [code="java"] /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * ...原创 2012-11-03 23:40:02 · 106 阅读 · 0 评论 -
Combinations
全排列 按理说很简单,可是用递归写,边界条件就还是难想清楚,sigh 主要是n原创 2012-11-03 22:19:25 · 82 阅读 · 0 评论 -
Combination Sum I && II
还是递归 但是边界条件以及边界上的处理不容易搞清楚(一开始我就把target==0的情况漏掉了,然后又把target==0放在了target原创 2012-11-03 21:41:08 · 80 阅读 · 0 评论 -
3Sum Closest
这题的思路和3Sum一样,只是要考虑一下k在j+1之后是不是需要减1,因为不是0而是绝对值最小,所以有点难考虑= = 我就懒得去想直接没-1 汗啊 [code="java"] public class Solution { public int threeSumClosest(int[] num, int target) { Arrays.sort(num); ...原创 2012-10-27 21:08:56 · 69 阅读 · 0 评论 -
4Sum
本来以为只要在3Sum外面再包一层循环就好了,可是。。。在Judge Large的时候还是超时了 呜呜呜 [code="java"] public class Solution { public ArrayList fourSum(int[] num, int target) { Arrays.sort(num); ArrayList result...原创 2012-10-27 22:49:56 · 47 阅读 · 0 评论 -
Add Binary
[code="java"] public String addBinary(String a, String b) { int x = Integer.parseInt(a, 2); int y = Integer.parseInt(b, 2); return Integer.toBinaryString(x + y); } [/code] 一开始直接用Int...原创 2012-10-29 00:07:02 · 55 阅读 · 0 评论 -
Add Two Numbers
这题不难 直接上递归就行 [code="java"] /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * n...原创 2012-10-30 23:03:48 · 60 阅读 · 0 评论 -
Anagrams
这题实在是没懂它的意思。。。囧啊 [code="java"] import java.util.HashSet; public class Solution { public ArrayList anagrams(String[] strs) { ArrayList results = new ArrayList(); HashSet anas ...原创 2012-10-31 00:33:34 · 93 阅读 · 0 评论 -
Balanced Binary Tree
[code="java"] /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public cl...原创 2012-11-01 23:38:29 · 55 阅读 · 0 评论 -
Best Time to Buy and Sell Stock I & II
啊 第一次直接过small和big测试 好爽!虽然主要是以前知道这个题目吧= = 以前一直想不清这个题目的算法,今天终于搞明白了。思路很简单:就是用i遍历第2到最后一天,看第i天卖最大的profit是多少。当然,这就需要维护一个前i项最小值的变量,很简单啦。这里还有一个问题,就是如果最大profit都小于0,那不如不卖。就像期货界里大家说的那样,你不交易,就已经超越80%的人了 = = 恩 代...原创 2012-11-02 22:05:03 · 80 阅读 · 0 评论 -
Binary Tree Inorder Traversal
I简单 直接递归就好 addAll函数很好用 [code="java"] /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x;...原创 2012-11-02 23:51:06 · 73 阅读 · 0 评论 -
climbing stairs
一开始觉得是简单的组合数学题,但是写完之后发现,首先组合数不是那么简单就能算好的,然后就是。。。。int对于up和down(分子和分母)太小了 囧啊 换成long好一点,还有错,然后再换成double,差不多了,但是有3个结果正好只差2,囧啊,改成float也不行,sigh [code="java"] public class Solution { public int climbS...原创 2012-11-03 17:18:22 · 116 阅读 · 0 评论 -
Insert Interval
各种条件真复杂,不仅是边界条件,而且还要分很多种情况讨论 而且 检查数组越界真心是个伤不起的活。。。 [code="java"] /** * Definition for an interval. * public class Interval { * int start; * int end; * Interval() { start = 0; e...原创 2012-11-11 01:33:33 · 98 阅读 · 0 评论