开发智力
sjphiChina
代码是最为耐心、最能忍耐和最令人愉快的伙伴,在任何艰难困苦的时刻,它都不会抛弃你。
展开
-
Combination Sum IV
这个最后得用dp来做,自己一开始想到的递归在遇到如[1,2,3],target=32时会超时,因为会有181997601种可能,laptop运行了1小时都还没算完。。。 public static int combinationSum3(int[] nums, int target) { int[] array = new int[target + 1];原创 2016-09-02 08:18:17 · 286 阅读 · 0 评论 -
Wiggle Sort II
再接着想0(n)的解法。public class Solution { public void wiggleSort(int[] nums) { if (nums == null || nums.length == 0) { return; } Arrays.sort(nums); int[] re原创 2016-07-26 14:15:40 · 250 阅读 · 0 评论 -
Maximum Product Subarray
坚持,坚持,坚持public class Solution { public int maxProduct(int[] nums) { if (nums.length == 1) { return nums[0]; } int maxProd = nums[0], maxTemp = nums[0], minTem原创 2016-06-18 04:45:25 · 219 阅读 · 0 评论 -
Shortest Word Distance III
坚持,坚持,坚持public class Solution { public int shortestWordDistance(String[] words, String word1, String word2) { int dist = words.length; int i = 0, i1 = -1, i2 = -1; for (;原创 2016-06-18 03:13:09 · 233 阅读 · 0 评论 -
Binary Tree Paths
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {原创 2016-07-14 20:18:42 · 192 阅读 · 0 评论 -
Regular Expression Matching
参考:点击打开链接对于这种几乎最高难度的题,甚于word ladder II,要放平心态。坚持,加油!public class Solution { public boolean isMatch(String s, String p) { if (p.length() == 0) { return s.length() == 0;原创 2016-07-04 09:02:21 · 275 阅读 · 0 评论 -
Maximum Product of Word Lengths
一道位操作的好题。当中的两个操作很好。public class Solution { public int maxProduct(String[] words) { if (words == null || words.length == 0) { return 0; } int[] elements = ne原创 2016-07-25 18:06:23 · 237 阅读 · 0 评论 -
The Skyline Problem
参考:点击打开链接要用最大堆,是保证,最高的楼总能展现在最外面,那些矮的楼都被覆盖了。循环中只要是左坐标,就要加入res中,只要这个左坐标还没有被用过,想这个例子就好了:[2, 10], [3, 15], [5, 12], [7, 15]遇到[5, 12],这时peek出来的是15(因为这是最大堆),而15在上次循环中已经被用过,压入了res中,而12刚被压入,还没有结束,所以不能原创 2016-07-03 09:21:00 · 308 阅读 · 0 评论 -
Paint House
想想想public class Solution { public int minCost(int[][] costs) { if (costs == null || costs.length == 0) { return 0; } for (int i = 1; i < costs.length; i++) { cost原创 2016-06-16 02:46:08 · 262 阅读 · 0 评论 -
Flip Game II
这道题不作要求public class Solution { public boolean canWin(String s) { if (s == null || s.length() == 0) { return false; } return helper(s.toCharArray()); }原创 2016-07-24 17:21:08 · 250 阅读 · 0 评论 -
Ugly Number II
参考:点击打开链接根据提示中的信息,我们知道丑陋数序列可以拆分为下面3个子列表:(1) 1×2, 2×2, 3×2, 4×2, 5×2, …(2) 1×3, 2×3, 3×3, 4×3, 5×3, …(3) 1×5, 2×5, 3×5, 4×5, 5×5, …仔细观察上述三个列表,我们可以发现每个子列表都是一个丑陋数分别乘以2,3,5,而要求的丑陋数就是从已经生原创 2016-07-24 16:43:38 · 228 阅读 · 0 评论 -
Ugly Number
参考:点击打开链接检测一个数是否为丑陋数,所谓丑陋数就是其质数因子只能是2,3,5。那么最直接的办法就是不停的除以这些质数,如果剩余的数字是1的话就是丑陋数了public class Solution { public boolean isUgly(int num) { while (num >= 2) { if (num % 2 == 0)原创 2016-07-24 16:15:20 · 249 阅读 · 0 评论 -
Factorial Trailing Zeroes
参考:点击打开链接public class Solution { public int trailingZeroes(int n) { int sum= 0; while (n > 0) { sum += n/5; n = n/5; } return sum; }}原创 2016-07-24 15:32:20 · 213 阅读 · 0 评论 -
Paint House II
参考:点击打开链接第一层for是对所有房子的遍历,第二层则是对某一个房子所有颜色的遍历。所以如果,第二个房子看到此时颜色与第一个房子的颜色相同(最小的cost),那就选择另一颜色(次小的)。每个房子都要找到自身和上一个房子最小的cost和次小的cost。理解:if (prevIdx == k) { costs[i][k] = costs[i][k原创 2016-07-01 13:58:02 · 346 阅读 · 0 评论 -
Longest Palindromic Substring
参考点击打开链接关键是第一个for循环,找到每个字符串的中心位置,然后以此中心位置向两边出发,public class Solution { public String longestPalindrome(String s) { if (s == null || s.length() == 0) { return ""; }原创 2016-06-15 14:38:40 · 219 阅读 · 0 评论 -
House Robber III
这道题一开始的确不好转化到tree的处理,这个学习了,最重要的是下面的错误,同上一题II,从根节点出发,这个无法变化,但若是从子节点出发,就完全可以是从子节点(这时相当于根)出发,或者,从子节点的子节点出发,所以必须要比较大小/** * Definition for a binary tree node. * public class TreeNode { * int val;原创 2016-06-15 11:42:36 · 238 阅读 · 0 评论 -
Single Number
判断两个,可以先加set,然后再从set里删掉啊!!bit之后做。public class Solution { public int singleNumber(int[] nums) { Set set = new HashSet<>(); for (int n: nums) { if (!set.add(n)) {原创 2016-06-18 15:05:21 · 167 阅读 · 0 评论 -
Counting Bits
先贴出一种自己马上写出的,更好的做法之后补上 public int[] countBits(int num) { if (num == 0) { return new int[]{0}; } int[] res = new int[num + 1]; for (int i = 1; i <= num;原创 2016-07-26 16:35:41 · 196 阅读 · 0 评论 -
Nested List Weight Sum II
学习了一种,先算最大的深度,再计算的思路。/** * // This is the interface that allows for creating nested lists. * // You should not implement it, or speculate about its implementation * public interface NestedInteger原创 2016-08-10 00:03:20 · 502 阅读 · 0 评论 -
Verify Preorder Sequence in Binary Search Tree
一道很好的拓展题,对于follow up,我们之后继续看。参考:点击打开链接public class Solution { public boolean verifyPreorder(int[] preorder) { int min = Integer.MIN_VALUE; Stack stack = new Stack<>();原创 2016-06-23 15:12:51 · 254 阅读 · 0 评论 -
Count Univalue Subtrees
参考:点击打开链接/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class S原创 2016-06-23 14:06:47 · 382 阅读 · 0 评论 -
Strobogrammatic Number II
参考:点击打开链接列出为0,1的情况,之后的情况皆是在此基础上产生:从中间插入来建立这个临时的字符串public class Solution { public List findStrobogrammatic(int n) { return findStrobogrammaticHelper(n, n); } private List f原创 2016-06-23 13:27:56 · 328 阅读 · 0 评论 -
Different Ways to Add Parentheses
把数字用符号隔开,用分治法求出所有可能。public class Solution { public List diffWaysToCompute(String input) { List result = new LinkedList<>(); for (int i = 0; i < input.length(); i++) {原创 2016-06-23 12:18:56 · 185 阅读 · 0 评论 -
Gas Station
一道智力题,抛去n方的解法,对于下面的做法,真的得能找到规律。当中的题眼是The solution is guaranteed to be unique.,于是i: 5,10,4,20,7c: 10,11,2,10,9就不满足,因为从4,或20都可以开始。既然unique,即如果前面所有的gas - cost加起来小于零,那么前面所有的点都不能作为出发点。参考:点击打开链原创 2016-06-21 17:37:40 · 189 阅读 · 0 评论 -
Surrounded Regions
这是的Number of Islands的先导题。以外围四条边为起始,找到当中的0,并从这个0开始找到与其联通的0,将这些0都置为特殊值,这些0都是不被x包围的。而于是剩下的0,则是被x包围。这中思维很重要。public class Solution { public void solve(char[][] board) { if (board == null原创 2016-06-21 16:57:17 · 182 阅读 · 0 评论 -
Partition List
这种链表的思路一定要掌握:两个dummy node各开一条临时链表,最后在拼接在一起。一定得记得去环啊!!/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x;原创 2016-06-21 13:03:16 · 188 阅读 · 0 评论 -
Sort Colors II
参考点击打开链接每次只对两个颜色min 和max排序,其他的都不变,于是中间再用I的方法即可求解。class Solution { /** * @param colors: A list of integer * @param k: An integer * @return: nothing */ public void sortCo原创 2016-07-17 01:51:57 · 362 阅读 · 0 评论 -
Best Time to Buy and Sell Stock III
把一个数组拆成两个数组,即处理两个stock I的问题,这样就求解了。left[0] 和right[length - 1] 都为0,因为这时各自都只剩下一个数组来处理了。很好的参考:点击打开链接public class Solution { public int maxProfit(int[] prices) { if (prices == null || pr原创 2016-07-17 01:10:58 · 228 阅读 · 0 评论 -
Best Time to Buy and Sell Stock II
思路源自小莹子点击打开链接,总是找到一个递减序列的最底部,然后找到递增序列的最高点,求差值。public class Solution { public int maxProfit(int[] prices) { if (prices == null || prices.length == 0) { return 0; }原创 2016-07-17 00:17:35 · 221 阅读 · 0 评论 -
Next Permutation
这个就是一定要理解如何求下一个排列的数学规律。用一个例子来说明,比如排列是(2,3,6,5,4,1),求下一个排列的基本步骤是这样:1) 先从后往前找到第一个不是依次增长的数,记录下位置p。比如例子中的3,对应的位置是1;2) 接下来分两种情况: (1) 如果上面的数字都是依次增长的,那么说明这是最后一个排列,下一个就是第一个,其实把所有数字反转过来即可(比如(6,5,4,3原创 2016-06-20 13:30:28 · 176 阅读 · 0 评论 -
Line Reflection
这道题我们要看清了,是平行于Y轴,而不是X轴。我知道我们很累,但也要看清楚题啊!!!public class Solution { public boolean isReflected(int[][] points) { int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE; if (points ==原创 2016-07-27 15:14:47 · 313 阅读 · 0 评论 -
Majority Element II
朴素的解法很简单,对于improved的,这个思维很好,得掌握。另外,那个是否为null的判断一定得做啊!!!!!!!public class Solution { public List majorityElement(int[] nums) { List list = new LinkedList<>(); if (nums == null |原创 2016-06-20 10:42:14 · 202 阅读 · 0 评论 -
Peeking Iterator
人家设置一个cache就解决问题啦,哈哈哈不用另置一个n的空间// Java Iterator interface reference:// https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.htmlclass PeekingIterator implements Iterator { Itera原创 2016-06-19 20:32:24 · 161 阅读 · 0 评论 -
House Robber II
这道题我们犯了两个大的错误:1. 忘记数量为1和2的情况了;2. 最关键的,我们一定要明确动归取值的意义。如此题,当start为0,表示可以从0这个位置开始,但从取值来看,未必就必须从0开始,如果1位置数值大,当然就可以从1开始,至于位置2,则是比较位置0加位置2的值和位置1的值,所以///// dp[1] = Math.max(dp[0], nums[1]);原创 2016-06-15 11:16:24 · 210 阅读 · 0 评论 -
Minimum Window Substring
传承自上一题,但又有很多要注意的地方。尤其是这个:map.put(s.charAt(prevIdx), map.get(s.charAt(prevIdx)) + 1); //if (map.get(s.charAt(prevIdx)) >= 0) { "bba", "ab" => "bba" i原创 2016-06-30 18:23:49 · 292 阅读 · 0 评论 -
Number of Islands
新知,扫描整个grid,将1的周围都设为0(dfs),这样有几个1,就有几个岛至于用union-find的解法,之后研习好了,可以先去吃午饭了public class Solution { public int numIslands(char[][] grid) { int num = 0; if (grid == null || grid.le原创 2016-06-11 03:19:11 · 296 阅读 · 0 评论 -
Subsets II
这个请好好理解i>0和i!=pos的区别,自己一开始认为是相同的效果,但其实是全然不同的对于:[1,2,2]前者:[[],[1],[1,2],[2]]后者:[[],[1],[1,2],[1,2,2],[2],[2,2]]public class Solution { public List> subsetsWithDup(int[] nums) { List原创 2016-06-10 13:56:14 · 301 阅读 · 0 评论 -
ZigZag Conversion
这是一道让自己呵呵呵呵的题,到现在依然还是红红火火恍恍惚惚哼哼哈嘿。当中这两个是关键点:1. int size = 2*numRows - 2;2.这个尤为不好懂,这个字符是紧接着size位置的字符(从行来看的话)int temp = j + size - 2*i;请参考:点击打开链接,人家还是个妹子!!!最后,即便都这样了,能不能不要犯下面的2个错误!!!public c原创 2016-06-10 12:15:26 · 178 阅读 · 0 评论 -
Bulls and Cows
这道题感觉也真是给自己新知,自己一开始有点蒙,尤其是想就一次循环把两只牛都求出来。一次求其实可以,但确实一上来有难度,这个在平时也就罢了,但在面试时咋办啊。退而求其次,就一点,找到一个能work的方法也行。下面的做法是参考了一种两次循环的比较好的。第一次for,求出bull的数量,同时将secret的其它字符连带数量存入map,第二次for,求出cow的数量,比对guess这种妥协原创 2016-06-10 08:17:37 · 196 阅读 · 0 评论 -
Intersection of Two Arrays II
这道题的非排序做法值得自己再好好看看,尤其是while循环中的三个条件非排序public class Solution { public int[] intersect(int[] nums1, int[] nums2) { int [] result = new int[0]; if (nums1 == null || nums1.length == 0 || nums2原创 2016-06-10 03:12:05 · 208 阅读 · 0 评论