从今天开始用Java写了
Easy
[338] 比特位计数
dynamic-programming | bit-manipulation
2022/04/07
class Solution {
public int[] countBits(int n) { int k = 1; int[] ans = new int[n + 1]; for (int i = 1; i <= n; i++) { if (i == k) { ans[i] = 1; k *= 2; } else { ans[i] = 1 + ans[i - k / 2]; } } return ans; } }
|
Medium
[279] 完全平方数
math | dynamic-programming | breadth-first-search
2022/04/05
class Solution {
public int numSquares(int n) { int[] dp = new int[n]; int k = 1; for (int i = 0; i < n; i++) { if (i + 1 == k * k) { dp[i] = 1; k++; } else { dp[i] = Integer.MAX_VALUE; for (int j = 1; j < k; j++) { dp[i] = Math.min(dp[i], 1 + dp[i - j * j]); } } } return dp[n - 1]; } }
|
[287] 寻找重复数
array | two-pointers | binary-search
2022/04/05
class Solution {
public int findDuplicate(int[] nums) { int fast = nums[nums[0]], slow = nums[0]; while (fast != slow) { fast = nums[nums[fast]]; slow = nums[slow]; } fast = 0; while (nums[slow] != nums[fast]) { fast = nums[fast]; slow = nums[slow]; } return nums[slow]; } }
|
[309] 最佳买卖股票时机含冷冻期
dynamic-programming
2022/04/06
class Solution {
public int maxProfit(int[] prices) { int n = prices.length; int[] buy = new int[n]; int[] sell = new int[n]; int[] frozen = new int[n]; buy[0] -= prices[0]; for (int i = 1; i < n; i++) { buy[i] = Math.max(frozen[i - 1] - prices[i], buy[i - 1]); sell[i] = Math.max(buy[i - 1] + prices[i], sell[i - 1]); frozen[i] = Math.max(sell[i - 1], frozen[i - 1]); } return Math.max(buy[n - 1], Math.max(sell[n - 1], frozen[n - 1])); } }
|
[337] 打家劫舍 III
tree | depth-first-search
2022/04/06
class Solution { private Map<TreeNode, Integer> chosenDp = new HashMap<>(); private Map<TreeNode, Integer> noChosenDp = new HashMap<>();
public int rob(TreeNode root) { dfs(root); return Math.max(chosenDp.getOrDefault(root, 0), noChosenDp.getOrDefault(root, 0)); }
private void dfs(TreeNode node) { if (node == null) { return; } else { dfs(node.left); dfs(node.right); chosenDp.put(node, node.val + noChosenDp.getOrDefault(node.left, 0) + noChosenDp.getOrDefault(node.right, 0)); noChosenDp.put(node, Math.max(chosenDp.getOrDefault(node.left, 0), noChosenDp.getOrDefault(node.left, 0)) + Math.max(chosenDp.getOrDefault(node.right, 0), noChosenDp.getOrDefault(node.right, 0))); } } }
|
[347] 前 K 个高频元素
hash-table | heap
2022/04/08
class Solution {
public int[] topKFrequent(int[] nums, int k) { Map<Integer, Integer> occurrences = new HashMap<Integer, Integer>(); for (int num : nums) { occurrences.put(num, occurrences.getOrDefault(num, 0) + 1); } PriorityQueue<int[]> queue = new PriorityQueue<int[]>(new Comparator<int[]>() { public int compare(int[] m, int[] n) { return m[1] - n[1]; } }); for (Map.Entry<Integer, Integer> entry : occurrences.entrySet()) { int num = entry.getKey(), count = entry.getValue(); if (queue.size() == k) { if (queue.peek()[1] < count) { queue.poll(); queue.offer(new int[] { num, count }); } } else { queue.offer(new int[] { num, count }); } } int[] ret = new int[k]; for (int i = 0; i < k; ++i) { ret[i] = queue.poll()[0]; } return ret; } }
|
[394] 字符串解码
stack | depth-first-search
2022/04/08
class Solution {
public String decodeString(String s) { StringBuffer ans = new StringBuffer(s); Stack<int[]> leftParenthesis = new Stack<>(); for (int i = 0; i < ans.length(); i++) { if (ans.charAt(i) == '[') { int num = 0, k = 1, j = i - 1; for (; j >= 0; j--) { if (ans.charAt(j) < '0' || ans.charAt(j) > '9') { break; } else { num += k * (ans.charAt(j) - '0'); k *= 10; } } int[] tmp = { j + 1, i, num }; leftParenthesis.add(tmp); } else if (ans.charAt(i) == ']') { int[] last = leftParenthesis.pop(); StringBuffer decodedString = new StringBuffer(); for (int j = 0; j < last[2]; j++) { decodedString.append(ans.substring(last[1] + 1, i)); } ans.replace(last[0], i + 1, decodedString.toString()); i = last[0] + decodedString.length() - 1; } } return ans.toString(); } }
|
Hard
[301] 删除无效的括号
depth-first-search | breadth-first-search
2022/04/06
class Solution {
public List<String> removeInvalidParentheses(String s) { List<String> ans = new ArrayList<>(); Set<String> bfs = new HashSet<>(); bfs.add(s); while (true) { for (String str : bfs) { if (isValid(str)) { ans.add(str); } } if (!ans.isEmpty()) { return ans; } Set<String> nextBfs = new HashSet<>(); for (String str : bfs) { for (int i = 0; i < str.length(); i++) { if (i > 0 && str.charAt(i) == str.charAt(i - 1)) { continue; } else if (str.charAt(i) == '(' || str.charAt(i) == ')') { nextBfs.add(str.substring(0, i) + str.substring(i + 1)); } } } bfs = nextBfs; } }
private Boolean isValid(String str) { int count = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == '(') { count++; } else if (str.charAt(i) == ')') { count--; if (count < 0) { return false; } } } return count == 0; } }
|
[312] 戳气球
divide-and-conquer | dynamic-programming
2022/04/05
class Solution {
public int maxCoins(int[] nums) { int n = nums.length; int[][] dp = new int[n + 2][n + 2]; int[] val = new int[n + 2]; val[0] = val[n + 1] = 1; for (int i = 1; i <= n; i++) { val[i] = nums[i - 1]; } for (int i = n - 1; i >= 0; i--) { for (int j = i + 2; j <= n + 1; j++) { for (int k = i + 1; k < j; k++) { int sum = val[i] * val[k] * val[j]; sum += dp[i][k] + dp[k][j]; dp[i][j] = Math.max(dp[i][j], sum); } } } return dp[0][n + 1]; } }
|