leetcode
暖洋洋的猫猫猫
刷题!
展开
-
Previous Permutation
思路和next permutation一样的。虽然楼主试了好久才让代码通过。。 class Solution { /** * @param nums: A list of integers * @return: A list of integers that's previous permuation */ public ArrayList<Intege原创 2015-11-23 07:06:42 · 310 阅读 · 0 评论 -
207 Course Schedule
参考了geeksforgeeks 有向图寻找环。代码如下:public class Solution { public boolean canFinish(int numCourses, int[][] prerequisites) { if (prerequisites == null || prerequisites.length == 0) {原创 2015-11-28 07:29:27 · 238 阅读 · 0 评论 -
159 Longest Substring with At Most Two Distinct Characters
The naive solution is to check all substrings and calculate the number of distinct characters they have. if the length of the string is n, then the time complexity of this solution will be n(n+1)/ 2, w原创 2015-11-22 14:19:49 · 298 阅读 · 0 评论 -
Combination Sum变体 II
order matters dynamic programmingpublic class DP { public int combinationSum(int[] candidates, int target) { Arrays.sort(candidates); if (target == 0) { return 0;原创 2015-11-23 11:59:18 · 269 阅读 · 0 评论 -
23 Merge N Sorted Lists
复习一下这道题。我一直都是用merge sort解法做的。今天在搜索别的解法时,发现这个文章讲的非常好: http://bangbingsyb.blogspot.com/2014/11/leetcode-merge-k-sorted-lists.html. 我的解法应该是属于第三种, time complexity nklog(k)public class Solution { publi原创 2015-11-23 07:51:53 · 266 阅读 · 0 评论 -
216 Combination Sum III
这道题目可以理解为,input是一个包含1-9的array,求出所有长度为k的,能够sum up成n的组合。每个数字在每个组合里只可以用一次,组合不可以重复。感觉像是combination和combination sum的合体。我还是打算用dfs做,那么难点就在于base case 和 recursion。 感觉这类题目都是在input的基础上递归,寻找以input里某个数字开头的结果。所以这道题原创 2015-11-23 04:06:28 · 294 阅读 · 0 评论 -
40 Combination Sum II
这道题目难点是去重。要求组合不可以重复出现,但是同一数字仍然可以多次使用。我就是在combination sum的基础上加了一行判断语句。代码如下:public class Solution { public List<List<Integer>> combinationSum2(int[] candidates, int target) { List<List<Intege原创 2015-11-23 03:01:32 · 227 阅读 · 0 评论 -
欢迎使用CSDN-markdown编辑器
欢迎使用Markdown编辑器写博客本Markdown编辑器使用StackEdit修改而来,用它写博客,将会带来全新的体验哦:Markdown和扩展Markdown简洁的语法代码块高亮图片链接和图片上传LaTex数学公式UML序列图和流程图离线写博客导入导出Markdown文件丰富的快捷键快捷键加粗 Ctrl + B 斜体 Ctrl + I 引用 Ctrl原创 2015-11-22 12:08:17 · 246 阅读 · 0 评论 -
Combination Sum变体[1]
Number of ways that you can get the target from a list of array. 重点内容 Order doesn’t matter:public class Solution { public int getNumOfWays(int[] inputs, int target) { if (inputs == null ||原创 2015-11-23 10:13:54 · 256 阅读 · 0 评论 -
115 Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none)原创 2015-11-22 13:14:15 · 701 阅读 · 0 评论 -
31 Next Permutation
听说这是一道经典题目,不过我没什么思路,看答案也不是那么容易懂。。。比较好的解释是 http://www.geeksforgeeks.org/find-next-greater-number-set-digits/我的代码是根据以上分析写的.代码有些冗长,好几次才通过。主要是卡在把最后一部分排序的地方,犯了不少低级错误。public class Solution { public void原创 2015-11-23 05:37:33 · 1305 阅读 · 0 评论 -
122 Best Time to Buy and Sell Stock II
与一非常类似。只需要改一个条件。public class Solution { public int maxProfit(int[] prices) { if (prices == null || prices.length <= 1) { return 0; } int max = Integer.MIN_VALUE;原创 2015-11-23 14:09:46 · 231 阅读 · 0 评论 -
121 Best Time to Buy and Sell Stock
这题是看算法导论学会的。。。。public class Solution { public int maxProfit(int[] prices) { if (prices == null || prices.length <= 1) { return 0; } int max = Integer.MIN_VALUE;原创 2015-11-23 14:05:41 · 160 阅读 · 0 评论 -
Basic Calculator
这道题目真是花了九牛二虎之力才做对。。。= = 先转成逆波兰式再计算。。。不多说了,直接上代码:public class Solution { public static int calculate(String s) { if (s == null || s.length() == 0) { return 0; } St原创 2015-12-04 12:46:14 · 261 阅读 · 0 评论