摘要: 荷兰三色旗问题。学以致用,理解更深刻了。顺便参看一下非常精简的一个解法,其实本质一样:http://discuss.leetcode.com/questions/251/sort-colorspublic class Solution { public void sortColors(int[] A) { // Start typing your Java solution below // DO NOT write main() function int len = A.length; if (len == 0) return; ... 阅读全文
posted @ 2013-08-05 22:03 阿牧遥 阅读(237) 评论(0) 推荐(0)
摘要: 组合DFS。对其中需要无重复,非降序的要求,只需做个小小的“剪枝”判断就行了。import java.util.ArrayList;import java.util.Collections; public class Solution { public ArrayList> combine(int n, int k) { // Start typing your Java solution below // DO NOT write main() function ArrayList> ans = new ArrayList>(); ... 阅读全文
posted @ 2013-08-05 21:34 阿牧遥 阅读(177) 评论(0) 推荐(0)
摘要: 子集DFS。注意:1. 层数是k还是k+1;2.Java中ArrayList的排序是Collections.sort(a)import java.util.ArrayList;import java.util.Collections;public class Solution { public ArrayList> subsets(int[] S) { // Start typing your Java solution below // DO NOT write main() function boolean[] status = new bo... 阅读全文
posted @ 2013-08-05 21:20 阿牧遥 阅读(303) 评论(0) 推荐(0)
摘要: 简单题。二分查找的应用。犯得错误是检查了数组下界0却忘了检查上界length。public class Solution { public boolean searchMatrix(int[][] matrix, int target) { // Start typing your Java solution below // DO NOT write main() function int m = matrix.length; if (m == 0) return false; int n = matrix... 阅读全文
posted @ 2013-08-05 20:05 阿牧遥 阅读(243) 评论(0) 推荐(0)
摘要: 先说思路。参考了这篇:http://blog.unieagle.net/2012/12/05/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Abest-time-to-buy-and-sell-stock-iii%EF%BC%8C%E4%B8%80%E7%BB%B4%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92/我们可以看到这个相当于要分两段,取前一段和后一段的分别最大值。这样分割点遍历,最后是O(n^2)的复杂度。然后优化后,使用一维的DP。可以降低到O(n)。也就是先从左到右扫一遍求得以i结尾的最大利润的数组,然后从右到左扫一遍求得以i开头的 阅读全文
posted @ 2013-08-05 19:40 阿牧遥 阅读(321) 评论(0) 推荐(0)
摘要: 一开始没理解对题意,估计是理解成III那道题的样子了。理解对了以后发现挺简单的。但看了参考之后发现它的解法更简洁优美,就直接照着来了。public class Solution { public int maxProfit(int[] prices) { // Start typ... 阅读全文
posted @ 2013-08-05 13:51 阿牧遥 阅读(185) 评论(0) 推荐(0)
摘要: 一趟遍历。O(n)。期间记录最小的价格min的值并不断比较更新最大差价。public class Solution { public int maxProfit(int[] prices) { // Start typing your Java solution below ... 阅读全文
posted @ 2013-08-05 12:20 阿牧遥 阅读(189) 评论(0) 推荐(0)