- 博客(33)
- 收藏
- 关注
原创 75. Sort Colors
public void sortColors(int[] nums) { if(nums == null || nums.length == 0 ||nums.length == 1) return; int lo = 0, hi = nums.length-1; for (int i = lo; i <= hi; i++) { if (nums[i] == 2) { exch(nums, i,
2020-07-30 19:16:15 54
原创 73. Set Matrix Zeroes
public void setZeroes(int[][] matrix) { if (matrix.length == 0 || matrix[0].length == 0) return; int m = matrix.length; int n = matrix[0].length; Set<Integer> rowSet = new HashSet<>(); Set<Integer>
2020-07-30 13:29:06 82
原创 60. Permutation Sequence
public String getPermutation(int n, int k) { if (n < 1) return ""; int maxK = 1; for (int i = 2; i <= n; i++) { maxK *= i; } if (k > maxK) return ""; List<String> res = new Array
2020-07-29 21:47:40 62
原创 45. Jump Game II
public int jump(int[] nums) { int n = nums.length; int jumps = 0; int currFurthest = 0; int currEnd = 0; for (int i = 0; i < n-1; i++) { currFurthest = Math.max(nums[i] + i, currFurthest);
2020-07-28 16:10:42 62
原创 55. Jump Game
public boolean canJump(int[] nums) { if (nums == null || nums.length < 2) return true; int n = nums.length; int last = n-1; for (int i = n-2; i >= 0; i--) { if (nums[i] + i >= last) { l
2020-07-28 15:30:07 69
原创 44. Wildcard Matching
public boolean isMatch(String s, String p) { if (s == null && p == null) { return true; } if (s == null || p == null) { return false; } boolean [][] dp = new boolean [s.length() + 1][p.length() + 1]; dp[0][0] = t
2020-07-28 14:32:35 63
原创 41. First Missing Positive
public int firstMissingPositive(int[] nums) { // 最终目的 nums[i] = i + 1 for (int i = 0; i < nums.length; i++) { if (nums[i] <= 0 || nums[i] > nums.length || nums[i] == i + 1 ||
2020-07-28 11:37:46 64
原创 31. Next Permutation
public void nextPermutation(int[] nums) { if (nums.length < 2) return ; int n = nums.length; int idx = n-1; while (idx > 0) { if (nums[idx - 1] < nums[idx]) break; idx--;
2020-07-27 21:44:43 51
原创 29. Divide Two Integers
public int divide(int dividend, int divisor) { if (dividend == Integer.MIN_VALUE && divisor == -1) return Integer.MAX_VALUE; long ans = 0; int sign = (dividend > 0) ^ (divisor > 0) ? -1 : 1; long dvd = Math.ab
2020-07-27 20:59:00 50
原创 最大在线人数,最大在线人数的最长在线时间区间是什么
class RangeTime { int startTime; int endTime; } public void f(List<RangeTime> rangeTimes) { int[] onlineNums = new int[24 * 60 * 60]; int maxOnlineNum = 0; int maxOnlineStart = 0, maxOnlineEnd = 0; int maxOnlineNumLast
2020-07-27 11:05:54 638
原创 162. Find Peak Element
public int findPeakElement(int[] nums) { if (nums == null || nums.length == 0) return -1; if (nums.length == 1) return 0; for (int i = 1; i < nums.length; i++) { if (nums[i] < nums[i-1]) return i-1;
2020-07-27 09:55:06 52
原创 460. LFU Cache
class LFUCache { HashMap<Integer, Integer> keyToVal; HashMap<Integer, Integer> keyToCount; HashMap<Integer, LinkedHashSet<Integer>> countToKeys; int cap; int min = -1; public LFUCache(int capacity) {
2020-07-26 04:16:09 56
原创 单调栈
Daily Temperatures medium Trapping Rain Water hard Largest Rectangle in Histogram hard
2020-07-26 03:30:38 75
原创 503. Next Greater Element II
public int[] nextGreaterElements(int[] nums) { int[] res = new int[nums.length]; int n = nums.length; Arrays.fill(res, -1); Deque<Integer> stack = new ArrayDeque<>(); for (int i = 0; i < 2 * n
2020-07-25 02:46:49 67
原创 496. Next Greater Element I
public int[] nextGreaterElement(int[] nums1, int[] nums2) { Map<Integer, Integer> map = new HashMap<>(); Deque<Integer> stack = new ArrayDeque<>(); int[] res = new int[nums1.length]; .
2020-07-25 02:30:00 51
原创 二分 binary
Search in Rotated Sorted Array 经典 Find First and Last Position of Element in Sorted Array 经典 Search Insert Position Pow(x, n) Sqrt(x) Search a 2D Matrix Search in Rotated Sorted Array II Find Minimum in Rotated Sorted Arra...
2020-07-24 15:11:33 76
原创 旋转有序数组最小元素下标 有序数组二分查找第一个>= target的下标
旋转有序数组 无重复元素 找到最小元素的下标 private int findMinIndex(int[] nums) { int lo = 0, hi = nums.length-1; while (lo < hi) { int mid = lo + (hi - lo) / 2; if (nums[mid] > nums[hi]) lo = mid + 1; else hi = mid; } return lo; } 有序数组 有重复元素
2020-07-24 11:37:27 236
原创 154. Find Minimum in Rotated Sorted Array II
public int findMin(int[] nums) { if (nums == null || nums.length == 0) return 0; int minIndex = findMinIndex(nums); return nums[minIndex]; } private int findMinIndex(int[] nums) { int lo = 0, hi = nums.
2020-07-24 11:22:45 49
原创 153. Find Minimum in Rotated Sorted Array
public int findMin(int[] nums) { if (nums == null || nums.length == 0) return 0; int minIndex = findMinIndex(nums); return nums[minIndex]; } private int findMinIndex(int[] nums) { int lo = 0, hi
2020-07-24 11:15:12 54
原创 81. Search in Rotated Sorted Array II
public boolean search(int[] nums, int target) { if (nums.length == 0) return false; int minIndex = findMinIndex(nums); int n = nums.length; int lo = target > nums[n-1] ? 0 : minIndex; int hi = target >
2020-07-24 11:09:07 49
原创 50. Pow(x, n)
public double myPow(double x, int n) { double res = 1; long absN = Math.abs((long)n); while (absN > 0) { if ((absN & 1) == 1) //奇数 res *= x; absN >>>= 1; x *= x; }
2020-07-23 16:04:09 64
原创 300. Longest Increasing Subsequence
public int lengthOfLIS(int[] nums) { if (nums == null || nums.length == 0) return 0; // dp[i] 以i坐标结尾的 列表 的 LIS长度 int[] dp = new int[nums.length]; dp[0] = 1; if (nums.length == 1) return 1; int maxAns = 0;
2020-07-02 17:47:13 71
原创 动态规划
5 Longest Palindromic Substring 53 Maximum Subarray 62 Unique Paths 63 Unique Paths II 64 Minimum Path Sum 70 Climbing Stairs 72 Edit Distance 91 Decode Ways 120 Triangle 121 Best Time to Buy and Sell Stock 139 Word Break 152 Maximum Product Sub.
2020-07-01 21:39:55 96
原创 264. 丑数 II
public int nthUglyNumber(int n) { if (n <= 0) return 0; if (n == 1) return 1; int[] dp = new int[n+1]; dp[1] = 1; int i2 = 1, i3 = 1, i5 = 1; for (int i = 2; i <= n; i++) { dp[i] = Math.min(2 * dp[i2], Math.min(3 * dp[i3], 5 * dp[i5]));
2020-07-01 21:10:18 75
原创 543. 二叉树的直径
// 最大直径不一定经过root int max = 0; public int diameterOfBinaryTree(TreeNode root) { if (root == null) return 0; getHeight(root); return max; } private int getHeight(TreeNode node) { if (node == null) return 0
2020-07-01 20:51:40 70
原创 263. Ugly Number
public boolean isUgly(int num) { if (num <= 0) return false; if (num == 1) return true; while (num % 2 == 0) num = num / 2; while (num % 3 == 0) num = num / 3; while (num % 5 == 0) num = num / 5; return num == 1; }
2020-07-01 20:31:03 67
原创 72. Edit Distance
public int minDistance(String word1, String word2) { int m = word1.length(); int n = word2.length(); int[][] dp = new int[m+1][n+1]; for (int i = 1; i <= m; i++) { dp[i][0] = i; } for (int j = 1; j <= n; j++) { dp[0][j] =
2020-07-01 20:14:19 69
原创 221. Maximal Square
public int maximalSquare(char[][] matrix) { if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return 0; int rows = matrix.length; int cols = matrix[0].length; int[][] dp = new int[rows][cols]; int max = 0; for (int i = 0;
2020-07-01 19:52:36 71
原创 213. House Robber II
private int robSub(int[] nums, int lo, int hi) { int curMax = 0, preMax = 0; for (int i = lo; i < hi; i++ ){ int t = curMax; curMax = Math.max(preMax + nums[i], curMax); preMax = t; }
2020-07-01 11:45:40 63
原创 198. House Robber
public int rob(int[] nums) { if (nums == null || nums.length == 0) return 0; int curMax = 0, preMax = 0; for (int i = 0; i < nums.length; i++ ){ int t = curMax; curMax = Math.max(preMax + nums[i], curMax
2020-07-01 11:34:15 66
原创 152. Maximum Product Subarray
public int maxProduct(int[] nums) { int res = nums[0]; for (int i = 1, imax = nums[0], imin = nums[0]; i < nums.length; i++) { if (nums[i] < 0) { int tmp = imax; imax = imin;
2020-07-01 11:21:31 91
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人