第二批
10/25 552. Student Attendance Record II 6 个状态间的转换
public int checkRecord(int n) { int M = 1000000007; long a0l0 = 1, a0l1 = 1, a0l2 = 0, a1l0 = 1, a1l1 = 0, a1l2 = 0; for (int i = 2; i <= n; i++) { long a0l0_t = (a0l0 + a0l1 + a0l2) % M; long a0l1_t = a0l0; long a0l2_t = a0l1; long a1l0_t = (a0l0 + a0l1 + a0l2 + a1l0 + a1l1 + a1l2) % M; long a1l1_t = a1l0; long a1l2_t = a1l1; a0l0 = a0l0_t; a0l1 = a0l1_t; a0l2 = a0l2_t; a1l0 = a1l0_t; a1l1 = a1l1_t; a1l2 = a1l2_t; } return (int)((a0l0 + a0l1 + a0l2 + a1l0 + a1l1 + a1l2) % M); }
10/26 645. Set Mismatch
public int[] findErrorNums(int[] nums) { for (int i = 0; i < nums.length; i++) { while (nums[i] - 1 != i && nums[nums[i] - 1] != nums[i]) { swap(nums, i, nums[i] - 1); } } for (int i = 0; i < nums.length; i++) { if (nums[i] - 1 != i) { return new int[]{nums[i], i + 1}; } } return new int[2]; } private void swap(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; }
10/27 423. Reconstruct Original Digits from English
public String originalDigits(String s) { int[] num = new int[10]; for (char c : s.toCharArray()) { if (c == 'z') num[0]++; if (c == 'o') num[1]++; if (c == 'w') num[2]++; if (c == 'h') num[3]++; if (c == 'u') num[4]++; if (c == 'f') num[5]++; if (c == 'x') num[6]++; if (c == 's') num[7]++; if (c == 'g') num[8]++; if (c == 'i') num[9]++; } num[1] = num[1] - num[0] - num[2] - num[4]; num[3] = num[3] - num[8]; num[5] = num[5] - num[4]; num[7] = num[7] - num[6]; num[9] = num[9] - num[5] - num[6] - num[8]; StringBuilder sb = new StringBuilder(); for (int i = 0; i <= 9; i++) { for (int j = 0; j < num[i]; j++) { sb.append(i); } } return sb.toString(); }
10/28 638. Shopping Offers
public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) { int res = 0; for (int i = 0; i < needs.size(); i++) { res += price.get(i) * needs.get(i); } for (int j = 0; j < special.size(); j++) { List<Integer> offer = special.get(j); boolean isV = true; for (int i = 0; i < needs.size(); i++) { if (offer.get(i) > needs.get(i)) { isV = false; } needs.get(i) -= offer.get(i); } if (isV) { res = Math.min(res, shoppingOffers(price, special, needs) + offer.get(offer.size() - 1)); } for (int i = 0; i < needs.size(); i++) { needs.get(i) += offer.get(i); } } return res; }
717. 1-bit and 2-bit Characters
public boolean isOneBitCharacter(int[] bits) { if (bits.length == 1 || bits.length == 2) { return bits[0] == 0; } boolean[] dp = new boolean[bits.length - 1]; dp[0] = bits[0] == 0; if (bits[1] == 0) { dp[1] = dp[0] || bits[0] == 1; } else { dp[1] = bits[0] == 1; } for (int i = 2; i < bits.length - 1; i++) { if (bits[i] == 0) { dp[i] = dp[i - 1] || (dp[i - 2] && bits[i - 1] == 1); } else { dp[i] = dp[i - 2] && bits[i - 1] == 1; } } return dp[bits.length - 2]; }
443. String Compression
public int compress(char[] chars) { int start = 0; int end = 0; int res = 0; int index = 0; while (end < chars.length) { char c = chars[start]; while (end + 1 < chars.length && chars[end + 1] == c) { end++; } int n = end - start + 1; chars[index++] = c; if (n > 1) { String str = n + ""; for (int i = 0; i < str.length(); i++) { chars[index++] = str.charAt(i); } } start = end + 1; end++; } return index; }
718. Maximum Length of Repeated Subarray
public int findLength(int[] A, int[] B) { int m = A.length; int n = B.length; int[][] dp = new int[m + 1][n + 1]; int res = 0; for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { if (i == 0 || j == 0) { dp[i][j] = 0; } else { if (A[i - 1] == B[j - 1]) { dp[i][j] = 1 + dp[i - 1][j - 1]; } } res = Math.max(dp[i][j], res); } } return res; }
719. Find K-th Smallest Pair Distance
public int smallestDistancePair(int[] nums, int k) { Arrays.sort(nums); int low = nums[1] - nums[0]; for (int i = 0; i < nums.length - 1; i++) { low = Math.min(low, nums[i + 1] - nums[i]); } int heigh = nums[nums.length - 1] - nums[0]; while (low < heigh) { int mid = low + (heigh - low) / 2; if (count(nums, mid) < k) { low = mid + 1; } else { heigh = mid; } } return low; } int count(int[] a, int k) { int res = 0; for (int i = 0; i < a.length; i++) { int j = i + 1; while (j < a.length && a[j] - a[i] <= k) { j++; } res += j - i - 1; } return res; }
617. Merge Two Binary Trees
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) { if (t1 == null && t2 == null) { return null; } if (t1 != null && t2 != null) { t1.val = t1.val + t2.val; t1.left = mergeTrees(t1.left, t2.left); t1.right = mergeTrees(t1.right, t2.right); return t1; } if (t1 == null && t2 != null) { return t2; } if (t1 != null && t2 == null) { return t1; } return null; }
10/30 665. Non-decreasing Array
public boolean checkPossibility(int[] nums) { if (nums.length <= 2) { return true; } int res = 0; if (nums[0] > nums[1]) { nums[0] = nums[1]; res++; } for (int i = 1; i < nums.length - 1; i++) { if (nums[i] > nums[i + 1]) { if (nums[i + 1] > nums[i - 1]) { nums[i] = nums[i + 1]; } else { nums[i + 1] = nums[i]; } res++; } } return res <= 1; }
11 /1 498. Diagonal Traverse
public int[] findDiagonalOrder(int[][] matrix) { int n = matrix.length; if (n == 0) { return new int[0]; } int m = matrix[0].length; if (m == 0) { return new int[0]; } int[] res = new int[n * m]; int x = 0, y = 0; int dx = -1, dy = 1; int index = 0; while (!(x == n - 1 && y == m - 1)) { res[index++] = matrix[x][y]; int nx = x + dx, ny = y + dy; if (nx >= 0 && nx < n && ny >= 0 && ny < m) { x = nx; y = ny; } else { if (dx == -1) { if (ny == m) { x = x + 1; y = y; } else { x = x; y = y + 1; } } else { if (nx == n) { x = x; y = y + 1; } else { x = x + 1; y = y; } } dx = -1 * dx; dy = -1 * dy; } } res[index] = matrix[n - 1][m - 1]; return res; }
10. Regular Expression Matching
public boolean isMatch(String s, String p) { int n = s.length(); int m = p.length(); boolean[][] dp = new boolean[n + 1][m + 1]; dp[0][0] = true; for (int i = 0; i < m; i++) { dp[0][i + 1] = i > 0 && dp[0][i - 1] && p.charAt(i) == '*'; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (p.charAt(j) == '*') { dp[i + 1][j + 1] = dp[i + 1][j - 1] || ((p.charAt(j - 1) == '.' || p.charAt(j - 1) == s.charAt(i)) && dp[i][j + 1]); } else { dp[i + 1][j + 1] = dp[i][j] && (s.charAt(i) == p.charAt(j) || p.charAt(j) == '.'); } } } return dp[n][m]; }
664. Strange Printer
public int strangePrinter(String s) { int n = s.length(); if (n == 0) { return 0; } int[][] dp = new int[101][101]; for (int i = 0; i < n; i++) { dp[i][i] = 1; } for (int h = 1; h < n; h++) { for (int i = 0; i < n - h; i++) { int j = i + h; dp[i][j] = h + 1; for (int k = i + 1; k <= j; k++) { int w = dp[i][k - 1] + dp[k][j]; if (s.charAt(k - 1) == s.charAt(j)) { w--; } dp[i][j] = Math.min(w, dp[i][j]); } } } return dp[0][n - 1]; }
412. Fizz Buzz
public List<String> fizzBuzz(int n) { List<String> res = new ArrayList<>(); if (n <= 0) { return res; } for (int i = 1; i <= n; i++) { if (i % 15 == 0) { res.add("FizzBuzz"); } else if (i % 3 == 0) { res.add("Fizz"); } else if (i % 5 == 0) { res.add("Buzz"); } else { res.add(i + ""); } } return res; }