Leetcode
文章平均质量分 56
再见小小ronnie
这个作者很懒,什么都没留下…
展开
-
Leetcode 62. Unique Paths
/** * for any point [i, j] in the matrix, # of paths is the sum of dp[i-1, j] and dp[i, j-1] * therefore, we only need to know the values above and on the left hand side of that point, * we use an原创 2016-09-18 23:44:52 · 193 阅读 · 0 评论 -
Leetcode 63. Unique Paths II
/** * similar with unique path problem, scan the matrix row by row * if obstacleGrid[i][j] == 1, then dp[j] = 0 * otherwise, just use same method as unique path which sums dp[j-1] and dp[j] then sa原创 2016-09-19 00:24:04 · 397 阅读 · 0 评论 -
Leetcode 96. Unique Binary Search Trees
Catalan numbers./** * # of binary trees = (# of trees on LHS) * (# of choices as root) * (# of trees on RHS) * let c[n] be the total # of binary trees * c[0] = c[1] = 1 * choose 1 as root, no e原创 2016-10-22 13:18:13 · 167 阅读 · 0 评论 -
Leetcode 120. Triangle
/** 1 * 1 2 * 1 2 3 * 1 2 3 4 * from bottom to top, we don't need to consider the last row (b/c if row # equals to 1 there's only one number just return this value), *原创 2016-11-03 11:52:15 · 204 阅读 · 0 评论 -
Leetcode 322. Coin Change
/** * given an array 'c[]' represents coins and a target value 'amount' * suppose the solution for 'amount' is sol(a) which refers to minimum # of conins to reach the amount * easy to know sol(a) = mi原创 2016-10-19 05:17:37 · 212 阅读 · 0 评论 -
Leetcode 375. Guess Number Higher or Lower II
@stupidbird911 said in [How does one think up DP solutions for these types of problems?](/post/122697):> Practice on similar questions, think carefully and you would get better understanding. Almost原创 2016-11-16 12:04:54 · 243 阅读 · 0 评论 -
Leetcode 338. Counting Bits
/** * num binary cnt * 0 0 0 * 1 1 1 * --------------------------------- * 2 10 1+0 * 3 11 1+1 * --------------------原创 2016-11-02 11:45:03 · 163 阅读 · 0 评论 -
Leetcode 416. Partition Equal Subset Sum
/** * given an array of positive integers and a target * find out if there exists a subset(s) that sum up to the target * subproblems: * 1. the subset includes nums[n] then return isSubsetSum(n-1,原创 2016-11-21 06:36:40 · 289 阅读 · 0 评论 -
Leetcode 357. Count Numbers with Unique Digits
/** * let f(n) to be the count of numbers with unique digits given the length of n * f(1) = 10 * f(2) = f(1) + 9*9, there are 9 choices (1 through 9) to form the first digit, and 9 choices to form原创 2016-12-15 04:10:42 · 160 阅读 · 0 评论 -
Leetcode 53. Maximum Subarray
/** * Kadane's algorithm */ public class Solution { public int maxSubArray(int[] nums) { int max_so_far = nums[0]; int max_ending_here = nums[0]; for (int i=1;原创 2016-12-15 13:23:53 · 182 阅读 · 0 评论 -
Leetcode 264. Ugly Number II
/** * k[0] = 1 * k[1] = min(k[0]*2, k[0]*3, k[0]*5) = 2 * k[2] = min(k[1]*2, k[0]*3, k[0]*5) = 3 * k[3] = min(k[1]*2, k[1]*3, k[0]*5) = 5 * k[4] = min(k[1]*2, k[1]*3, k[1]*5) = 4 * ... */ publ转载 2016-12-16 07:35:37 · 182 阅读 · 0 评论 -
Leetcode 300. Longest Increasing Subsequence
/** * let dp[i] be the length of the longest sub-array ending with nums[i] * then dp[n] = dp[m]+1 if nums[n] > nums[m] AND dp[m] is the largest (m is from 1 to n-1) * else dp[n] = 1 */ publi原创 2016-12-17 07:47:24 · 158 阅读 · 0 评论 -
Leetcode 66. Plus One
public class Solution { public boolean isCarry(int num) { return (num/10 == 1) ? true : false; } public int[] plusOne(int[] digits) { int i = digits.length-1;原创 2016-12-21 12:10:59 · 126 阅读 · 0 评论 -
Leetcode 217. Contains Duplicate
got a TLE using HashMappublic class Solution { public boolean containsDuplicate(int[] nums) { HashMap map = new HashMap(); for (int num : nums) { if (map.get(num) !=原创 2016-12-21 12:28:04 · 171 阅读 · 0 评论 -
Leetcode 91. Decode Ways
/** * allocate an array w[n] to save ways of decryption for a message which length is n * suppose a1a2...an-1 is the first n-1 digits of the encoded message and has w[n-1] ways to decode * then for原创 2016-10-22 07:21:57 · 224 阅读 · 0 评论 -
Leetcode 309. Best Time to Buy and Sell Stock with Cooldown
State machine graph/** * state machine approach * state a(hold): hold stack, sell or rest * state b(sold): just sold stack can only rest * state c(rest): cooldown, buy or rest * translate to for原创 2016-12-18 10:57:34 · 150 阅读 · 0 评论 -
Leetcode 122. Best Time to Buy and Sell Stock II
public class Solution { public int maxProfit(int[] prices) { if (prices.length < 2) return 0; int profits = 0; for (int i=1; i<prices.length; i++) if (prices[i]转载 2016-12-22 06:33:59 · 132 阅读 · 0 评论 -
Leetcode 153. Find Minimum in Rotated Sorted Array
/** * binary search approach, o(logN) */ public class Solution { public int findMin(int[] nums) { int low = 0, high = nums.length-1, mid; while (low < high) { mid =原创 2016-12-22 07:24:21 · 167 阅读 · 0 评论 -
Leetcode 162. Find Peak Element
O(n) approachpublic class Solution { public int findPeakElement(int[] nums) { int peak = nums[0]; for (int i=1; i<nums.length; i++) { if (nums[i] > peak) peak = num原创 2016-12-22 10:49:36 · 168 阅读 · 0 评论 -
Leetcode 167. Two Sum II - Input array is sorted
/** * Two-pointer approach. * pointer low starts from 0, pointer high starts from length-1 * if nums[low]+nums[high] == target return low and high * else if nums[low]+nums[high] > target means we原创 2016-12-22 12:32:58 · 176 阅读 · 0 评论 -
Leetcode 11. Container With Most Water
/** * Two pointer. * Left and right are the two pointers indicate tallest lines, * whenever there is a taller line update the corresponding pointer. * Note that the volumn is determined by the sho原创 2017-01-02 02:49:57 · 116 阅读 · 0 评论 -
Leetcode 15. 3Sum
/** * First sort the array, (i) * scan through the array, set every -nums[i] as a target, (ii) * for the rest elements i+1<=i<=n do a two-pointer two sum原创 2017-01-02 06:26:56 · 154 阅读 · 0 评论 -
Leetcode 18. 4Sum
public class Solution { // two-pointer to search three numbers sum to target public static void threeSum(List> res, int start, int goal, int target, int[] nums) { for (int i=start; i<n原创 2017-01-02 11:26:39 · 202 阅读 · 0 评论 -
Leetcode 31. Next Permutation
/** * e.g. 1 2 3 4 * Step i. From right to left, find the first element at index i which breaks the ascending order, * if no such i exists, then the array is sorted as ascending order, return the原创 2017-01-03 09:41:17 · 156 阅读 · 0 评论 -
Leetcode 34. Search for a Range
/** * Two binary search. * When isLeft is true search the left index of the target, * else search the right index of the index. */public class Solution { public static int helper(int[] num原创 2017-01-03 10:33:06 · 199 阅读 · 0 评论 -
Leetcode 209. Minimum Size Subarray Sum
/** * Using two pointers to maintain a window * the left pointer is used to add number to the sum, * the right pointer is used to cut number from the sum, * when sum >= s, save the length to recor原创 2016-12-23 03:37:11 · 144 阅读 · 0 评论 -
Leetcode 35. Search Insert Position
public class Solution { public int searchInsert(int[] nums, int target) { int low = 0, high = nums.length-1, mid = 0; while (low <= high) { mid = (low+high) >> 1;原创 2017-01-03 13:04:21 · 153 阅读 · 0 评论 -
Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {原创 2017-01-03 14:24:53 · 262 阅读 · 0 评论 -
Leetcode 228. Summary Ranges
public class Solution { public List summaryRanges(int[] nums) { int i = 1, j = 0; List res = new ArrayList(); if (nums.length == 0) return res; while (i < nums.leng原创 2016-12-24 02:06:12 · 171 阅读 · 0 评论 -
Leetcode 414. Third Maximum Number
public class Solution { public int thirdMax(int[] nums) { long first = Long.MIN_VALUE, second = Long.MIN_VALUE, third = Long.MIN_VALUE; // fi原创 2016-12-25 06:37:01 · 207 阅读 · 0 评论 -
Leetcode 368. Largest Divisible Subset
/** * similar with the Longest Increasing Subsequence (LIS) * let l[i] to be the largest divisible subset ending at index i * to determine l[i+1] we need to do, * i. for every list l[j] in l[1] t转载 2016-12-19 07:32:06 · 144 阅读 · 0 评论 -
Leetcode 75. Sort Colors
o(n) two-passpublic class Solution { public void sortColors(int[] nums) { if (nums.length == 0) return; int cnt1 = 0, cnt2 = 0, cnt3 = 0; for (int num : nums) {原创 2016-12-26 01:47:52 · 134 阅读 · 0 评论 -
Leetcode 33. Search in Rotated Sorted Array
Using the find minimum discussed in Leetcode 153. Find Minimum in Rotated Sorted Array. After found the minimum, apply binary search on the sorted array. public class Solution { // find the原创 2016-12-27 02:03:46 · 165 阅读 · 0 评论 -
Leetcode 81. Search in Rotated Sorted Array II
/** * similar with Search in Rotated Sorted Array, * the difference is we need to take care of the case such as * 1, 1, ..., 1, 1 here, 1 is the duplicate element. * The algorithm discussed in t原创 2016-12-27 07:22:53 · 140 阅读 · 0 评论 -
Leetcode 33. Search in Rotated Sorted Array
public class Solution { // find the index of the minimum public int searchMin (int[] nums) { int low = 0, high = nums.length-1, mid = 0; while (low < high) { mid =原创 2016-12-27 07:25:08 · 129 阅读 · 0 评论 -
Leetcode 81. Search in Rotated Sorted Array II
/** * similar with Search in Rotated Sorted Array, * the difference is we need to take care of the case such as * 1, 1, ..., 1, 1 here, 1 is the duplicate element. * The algorithm discussed in t原创 2016-12-27 07:25:38 · 138 阅读 · 0 评论 -
Leetcode 1. Two Sum
public class Solution { public int[] twoSum(int[] nums, int target) { // intuitive O(n^2) approach for (int i=0; i<nums.length; i++) for (int j=i+1; j<nums.le原创 2016-12-20 02:09:35 · 93 阅读 · 0 评论 -
Leetcode 26. Remove Duplicates from Sorted Array
public class Solution { public int removeDuplicates(int[] nums) { if (nums.length == 0) return 0; int pre = nums[0]; int idx = 1; int len = 1;原创 2016-12-20 04:07:17 · 106 阅读 · 0 评论 -
Leetcode 80. Remove Duplicates from Sorted Array II
public class Solution { public int removeDuplicates(int[] nums) { if (nums.length == 0) return 0; int pre = nums[0]; int idx = 1; int len = 1; in原创 2016-12-20 04:13:24 · 98 阅读 · 0 评论 -
Leetcode 216. Combination Sum III
public class Solution { private static int rounds; public static void backTrack(int k, int n, int start, int[] nums, List tmp, List> res) { if (k == 0) return; else {原创 2017-01-05 04:52:39 · 170 阅读 · 0 评论