LeetCode Redo
文章平均质量分 66
chiiis
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Flatten Binary Tree to Linked List 二叉树变成链表@LeetCode,
package Level4; import Utility.TreeNode; /** * Flatten Binary Tree to Linked List * * Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \原创 2013-11-18 06:44:36 · 2911 阅读 · 0 评论 -
Integer to Roman 阿拉伯数字转罗马数字@LeetCode
package Level2; /** * Integer to Roman * * Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. * */ public class S12 { public static原创 2013-10-25 02:46:46 · 2246 阅读 · 2 评论 -
Maximum Subarray 最大子数组和(连续)@LeetCode
package Level3; import java.util.Arrays; /** * * Maximum Subarray * * Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, give原创 2013-11-08 04:54:46 · 6667 阅读 · 2 评论 -
Evaluate Reverse Polish Notation 求RPN的值@LeetCode
用一个Stack就很容易做到了,这道题的最大发现是:这道题是2013-11-27发布出来的,到今天,短短半个月内,居然提交量快到了1万!!! 居然有这么多人都在刷题!!压力山大! package Level3; import java.util.Stack; /** * * Evaluate Reverse Polish Notation * * Evaluate t原创 2013-12-15 09:07:28 · 4522 阅读 · 2 评论 -
Merge Intervals 合并区间@LeetCode
采用的是和Insert Interval一样的思想,只不过最开头要先排序一下,用到了java的Collections.sort(List list, Comparator c) 自己实现了一个Comparator的compare方法 package Level4; import java.util.ArrayList; import java.util.Collections; im原创 2013-11-22 12:42:36 · 5082 阅读 · 3 评论 -
Insert Interval 插入区间@LeetCode
动态合并的问题: 通过遍历intervals,和newInterval对比: 1 如果比newInterval小:直接加入新集合 2 如果有overlap,动态改变newInterval为新的区间,继续合并 3 如果比newInterval大:加入newInterval到新集合,然后把newInterval更新为当前对象 之前在做第三步时,不懂得用动态改变newInterval的原创 2013-11-22 07:46:27 · 2782 阅读 · 0 评论 -
Two Sum 数组中找两个之和为sum@LeetCode
package Level2; import java.util.HashMap; import java.util.Hashtable; /** * Two Sum * * Given an array of integers, find two numbers such that they add up to a specific target number. The funct原创 2013-10-25 04:06:34 · 2900 阅读 · 0 评论 -
Sqrt(x) 求平方根@LeetCode
package Level4; /** * Sqrt(x) * * Implement int sqrt(int x). * * Compute and return the square root of x. * */ public class S69 { public static void main(String[] args) { System.out.pri原创 2013-11-11 02:58:06 · 2993 阅读 · 1 评论 -
4Sum 找4个数之和为target@LeetCode
模板化了,要计算 k-sum 复杂度为O(N^(k-1)) package Level3; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** * 4Sum * Given an array S of n integer原创 2013-11-04 07:18:51 · 2266 阅读 · 0 评论 -
3Sum 数组找3数之和为0 @LeetCode
排序,二分法查找,前后双指针 package Level3; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** * 3Sum * Given an array S of n integers, are there el原创 2013-11-04 05:32:24 · 3538 阅读 · 0 评论 -
Roman to Integer 罗马数字转阿拉伯数字@LeetCode
package Level2; /** * Roman to Integer * * Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. */ public class S13 { public static voi原创 2013-10-25 02:42:49 · 1770 阅读 · 0 评论 -
Pow(x, n) 指数 @LeetCode
package Level3; /** * Pow(x, n) * Implement pow(x, n). * */ public class S50 { public static void main(String[] args) { System.out.println(pow(2, -2)); } public static double pow(double原创 2013-11-06 03:28:48 · 1818 阅读 · 0 评论 -
3Sum Closest 找3个数使得和最接近的target @LeetCode
与上题差不多,只是不能再用系统自带的二分法,要自己写一个。 package Level3; import java.util.Arrays; /** * 3Sum Closest * * Given an array S of n integers, find three integers in S such that the sum is closest to a g原创 2013-11-04 05:54:46 · 1937 阅读 · 0 评论 -
Subsets 求子集 @LeetCode
package Level3; /** * Subsets * * Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain原创 2013-11-06 07:18:15 · 3049 阅读 · 1 评论 -
逆波兰表达式转换 RPN infix
首先参考这篇:http://blog.youkuaiyun.com/sgbfblog/article/details/8001651 一、后缀表达式求值 后缀表达式也叫逆波兰表达式,其求值过程可以用到栈来辅助存储。假定待求值的后缀表达式为:6 5 2 3 + 8 * + 3 + *,则其求值过程如下: 1)遍历表达式,遇到的数字首先放入栈中,此时栈如下所示: 2)接着读原创 2013-12-20 04:19:36 · 1704 阅读 · 0 评论 -
String to Integer (atoi) 转换string为整数@LeetCode
挺tricky的一题,这题我没有用正常的方法来做,用了挺邪恶的方法: 1 用正则表达式过滤掉前面的whitespace 2 用Long去parseLong,这里还遇到奇怪的问题,当parseLong传+号时,在本机跑没问题,一到OJ上就会Runtime Exception! 3 用了Exception做检查 这题肯定有比较正常的做法,但也许这是最直观的方法了 pack原创 2013-10-25 13:24:43 · 2117 阅读 · 2 评论 -
Celebrity Problem
Celebrity Problem import java.util.Stack; /** In a party of N people, only one person is known to everyone. Such a person may be present in the party, if yes, (s)he doesn’t know anyone in t原创 2013-12-19 10:26:45 · 3820 阅读 · 0 评论 -
Text Justification 文本对齐 @LeetCode
快被这道题搞死了。。 根据这篇重写了 http://www.cnblogs.com/TenosDoIt/p/3475275.html 分析:这一题需要注意两个点,a、当该行只放一个单词时,空格全部在右边 b、最后一行中单词间只有一个空格,其余空格全部在右边。然后只要贪心选择,在一行中尽量放多的单词。 package Level4; import java.util.ArrayL原创 2013-12-21 06:14:45 · 4510 阅读 · 0 评论 -
Combinations 求1...n中k长度的组合 @LeetCode
和之前的那篇permutation类似,同样的递归思想 package Level3; import java.util.ArrayList; /** * Combinations * * Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.原创 2013-11-06 06:46:42 · 2991 阅读 · 1 评论 -
Search a 2D Matrix 在有序二维矩阵查找数 @LeetCode
经典面试题,从右上角开始查找O(m+n),可以不用二分法 package Level3; /** * Search a 2D Matrix * *Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:原创 2013-11-06 06:12:02 · 2230 阅读 · 0 评论 -
Binary Tree Level Order Traversal II 二叉树按层遍历(反向输出)@LeetCode
树的Level Order Traversal加上ArrayList翻转 package Level3; import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import Utility.TreeNode; /** * Binary Tree Level Order T原创 2013-11-21 03:01:06 · 5192 阅读 · 0 评论 -
Binary Tree Level Order Traversal 分层遍历二叉树@LeetCode
package Level3; /** * Binary Tree Level Order Traversal * * Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: G原创 2013-11-08 13:20:32 · 5153 阅读 · 0 评论 -
Combination Sum II 无序数组中找组合(每个元素只能用一次)使得和为target@LeetCode@LeetCode
和上一题思想一样,区别在于每个元素只能用一次,所以start的位置也要改变。 另外一个问题是如何处理返回大集合中有重复子集合的问题。最容易想到的就是用HashSet来过滤一遍,参考了http://blog.youkuaiyun.com/u011095253/article/details/9158423 的大作后发现有一种更绿色的方法,就是多用一个while循环跳过相同的元素!这个技巧在很多情况下都很好用原创 2013-11-21 12:40:25 · 2555 阅读 · 0 评论 -
Permutations 排序 @LeetCode
package Level3; import java.util.ArrayList; /** * Permutations * * Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [1,2,3原创 2013-11-06 01:21:15 · 3084 阅读 · 0 评论 -
Permutations 排序(有重复数)II @LeetCode
比起http://blog.youkuaiyun.com/fightforyourdream/article/details/14217105 多加了一个while来去重,发现这个去重方法在另一道题也用过,同样也是DFS里面去重,很好用! 另外就是在最前面加了一个sort,因为如果没加,当输入乱序时就会Output Limit Exceed! package Level4; import ja原创 2013-12-20 14:41:26 · 2918 阅读 · 0 评论 -
Combination Sum 无序数组中找组合(每个元素可用多次)使得和为target@LeetCode
典型DFS package Level3; import java.util.ArrayList; import java.util.Arrays; /** * * Combination Sum * * Given a set of candidate numbers (C) and a target number (T), find all unique * co原创 2013-11-21 12:23:56 · 2029 阅读 · 0 评论 -
Valid Number 判断一个string是不是有效地数字表达式 @LeetCode
这道题看了看网上的解法,很多都做得很麻烦。其实我觉得,这道题就是考察正则表达式。 package Level2; /** * Valid Number * * Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" =>原创 2013-10-21 02:55:33 · 15933 阅读 · 5 评论
分享