- 博客(54)
- 收藏
- 关注
原创 Leetcode 367. Valid Perfect Square
public class Solution { public boolean isPerfectSquare(int num) { long mid = 0; int low = 1, high = num; while (low <= high) { mid = (low+high)>>1;
2016-12-31 15:16:58 131
转载 Leetcode 268. Missing Number
Reference.public class Solution { public int missingNumber(int[] nums) { int diff = 0; for (int i=0; i<nums.length; i++) diff += nums[i]-i; return nums.length
2016-12-31 14:44:20 132
原创 Leetcode 74. Search a 2D Matrix
public class Solution { public boolean searchMatrix(int[][] matrix, int target) { int row = matrix.length; if (row == 0) return false; int col = matrix[0].length;
2016-12-31 13:55:59 140
原创 Leetcode 73. Set Matrix Zeroes
Reference.public class Solution { public void setZeroes(int[][] matrix) { int row = matrix.length; int col = matrix[0].length; // determine if the first row and
2016-12-31 13:11:39 205
转载 Leetcode 59. Spiral Matrix II
public class Solution { public int[][] generateMatrix(int n) { int[][] res = new int[n][n]; int up = 0, right = n-1, down = n-1, left = 0, elem = 1; while (true) {
2016-12-31 11:46:04 132
转载 Leetcode 54. Spiral Matrix
/** * Intuitive solution. * Just add the spiral sequence to the result. */ public class Solution { public List spiralOrder(int[][] matrix) { List seq = new ArrayList<>();
2016-12-31 11:29:00 116
原创 Leetcode 48. Rotate Image
/** * Example, matrix[i][j] 0=<i<=3, 0=<j<=3 * * 1 2 3 4 * 5 6 7 8 * 9 10 11 12 * 13 14 15 16 * * Transpose the matrix by switching i and j to matrix[j][i], * * 1 5
2016-12-31 07:29:59 154
转载 Leetcode 40. Combination Sum II
public class Solution { public static void backTrack(List tmp, List> res, int start, int target, int[] nums) { if (target < 0) return; else if (target == 0) res.add(new ArrayList<>
2016-12-31 06:38:46 179
转载 Leetcode 39. Combination Sum
Backtracking. public class Solution { public static void backTrack(List tmp, List> res, int start, int target, int[] nums) { if (target < 0) return; else if (target == 0) res.add
2016-12-31 06:21:46 164
转载 Big O and little o
O(n) means,For at least one choice of a constant k > 0, you can find a constant a such that the inequality 0 <= f(x)k g(x) holds for all x > a.o(n) means,For every choice of a constant k
2016-12-31 01:13:21 1116
原创 Leetcode 45. Jump Game II
BFS solution for reference. /** * Breadth first search o(n). * Map each number in the array to a level, * where numbers in level i are all the numbers that can be reached in i-1th jump. * e.g. 2
2016-12-31 01:08:12 144
原创 Leetcode 55. Jump Game
Simple recursion solution but got TLE.public class Solution { private boolean isJump = false; public void canJumpRecur(int cur, int k, int[] nums) { if (cur == nums.length-1) {
2016-12-28 12:01:52 134
原创 Leetcode 442. Find All Duplicates in an Array
/** * As described in the problem, * 1 ≤ a[i] ≤ n (n = size of array) * therefore, we can use nums[nums[i]] = -nums[nums[i]] to help us which elements * appear twice. Note that elements in the a
2016-12-28 10:22:30 151
原创 Leetcode 238. Product of Array Except Self
http://stackoverflow.com/questions/2680548/given-an-array-of-numbers-return-array-of-products-of-all-other-numbers-no-div/** * The product of an index equals to the product left to the indx times the
2016-12-28 09:40:21 141
转载 Leetcode Majority Element II Extension
Extension of Majority Element II.Majority element is defined as numbers appear more than n/k times. public class Solution { public List majorityElement(int[] nums) { int n = nums.length, k
2016-12-28 07:27:16 187
原创 Leetcode 229. Majority Element II
public class Solution { public List majorityElement(int[] nums) { List res = new ArrayList(); if (nums.length == 0) return res; int candidate1 = 0, count1 = 0;
2016-12-28 07:19:51 153
原创 Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution {
2016-12-28 03:00:07 145
原创 Leetcode 90. Subsets II
NOT ACCEPTED CODEIdea is sort the array to make duplicates adjacent.If the current number is same as the previous one, then we only need to add current number to the last added subset. e.g. [1,
2016-12-27 11:57:28 150
原创 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 145
原创 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 138
原创 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 150
原创 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 171
转载 Leetcode 78. Subsets
public class Solution { public List> subsets(int[] nums) { List> res = new ArrayList<>(); res.add(new ArrayList()); Arrays.sort(nums); for (int num : nums)
2016-12-26 05:44:16 153
原创 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 140
原创 Leetcode 448. Find All Numbers Disappeared in an Array
o(n) time and spacepublic class Solution { public List findDisappearedNumbers(int[] nums) { List list = new ArrayList(); int[] res = new int[nums.length]; for (int num :
2016-12-25 08:27:53 276
原创 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 214
原创 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 181
原创 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 157
原创 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 185
原创 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 175
原创 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 192
转载 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 138
原创 Leetcode 283. Move Zeroes
naive o(n^2) approach like bubble sortpublic class Solution { public void moveZeroes(int[] nums) { int j, len = nums.length; for (int i=0; i<len; i++) if (n
2016-12-21 13:37:27 149
原创 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 176
原创 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 133
原创 Leetcode 88. Merge Sorted Array
public class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int i=0, j=0, k=0; int[] tmp = new int[m+n]; while (i<m && j<n) { if (nums1[
2016-12-21 08:12:15 152
原创 Leetcode 219. Contains Duplicate II
public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { boolean isContain = false; HashMap map = new HashMap(); for (int i=0; i<nums.le
2016-12-20 13:22:06 124
原创 Leetcode 189. Rotate Array
public class Solution { public void rotate(int[] nums, int k) { int len = nums.length; // in case k is larger than len int t = k % len; if (t == 0
2016-12-20 12:56:20 136
原创 Leetcode 169. Majority Element
public class Solution { public int majorityElement(int[] nums) { int i=1; int len = 1; Arrays.sort(nums); for (i=1; i<nums.length; i++) { if (nums[i] ==
2016-12-20 12:05:37 106
原创 Leetcode 119. Pascal's Triangle II
/** * 1 * 1 1 * 1 2 1 * 1 3 3 1 * 1 4 6 4 1 */ public class Solution { public List getRow(int rowIndex) { List pt = new ArrayList();
2016-12-20 06:57:11 112
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人