classSolution{ publicintmaxProduct(int[] nums){ int max = nums[0], min = nums[0], ans = nums[0]; for (int i = 1; i < nums.length; i++) { int temp = max; max = Math.max(Math.max(max * nums[i], min * nums[i]), nums[i]); min = Math.min(Math.min(temp * nums[i], min * nums[i]), nums[i]); ans = Math.max(ans, max); } return ans; } }
单词拆分
给定一个非空字符串 s 和一个包含非空单词的列表 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。
// 双向队列 publicclassMain{ publicstaticvoidmain(String[] args){ Scanner sc = new Scanner(System.in); String input = sc.next(); Deque<Integer> nums = new LinkedList<>(); Deque<Character> opes = new LinkedList<>(); int sum = 0; for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); if (c >= '0' && c <= '9') { sum = sum * 10 + (c - '0'); } else { nums.offerFirst(sum); sum = 0; if (opes.isEmpty()) { opes.offerFirst(c); } else { if (opes.peekFirst() == '*') { opes.pollFirst(); int num2 = nums.pollFirst(); int num1 = nums.pollFirst(); int res = num1 * num2; nums.offerFirst(res); } elseif (opes.peekFirst() == '/') { opes.pollFirst(); int num2 = nums.pollFirst(); int num1 = nums.pollFirst(); int res = num1 / num2; nums.offerFirst(res); } opes.offerFirst(c); } } } nums.offerFirst(sum); if (opes.peekFirst() != null) { if (opes.peekFirst() == '*') { opes.pollFirst(); int num2 = nums.pollFirst(); int num1 = nums.pollFirst(); int res = num1 * num2; nums.offerFirst(res); } elseif (opes.peekFirst() == '/') { opes.pollFirst(); int num2 = nums.pollFirst(); int num1 = nums.pollFirst(); int res = num1 / num2; nums.offerFirst(res); } } while (nums.size() > 1) { int num1 = nums.pollLast(); int num2 = nums.pollLast(); char c = opes.pollLast(); if (c == '+') { int res = num1 + num2; nums.offerLast(res); } elseif (c == '-') { int res = num1 - num2; nums.offerLast(res); } } System.out.println(nums.peekFirst()); } }
// DFS classSolution{ publicintnumIslands(char[][] grid){ int count = 0; for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[0].length; j++) { if (grid[i][j] == '1') { func(grid, i, j); count++; } } } return count; }
publicvoidfunc(char[][] grid, int x, int y){ if (grid[x][y] != '1') { return; } grid[x][y] = '2'; int[][] direction = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}}; for (int[] d : direction) { int nx = x + d[0]; int ny = y + d[1]; if (nx < 0 || nx >= grid.length || ny < 0 || ny >= grid[0].length) { continue; } func(grid, nx, ny); } } }
classSolution{ publicbooleanexist(char[][] board, String word){ for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[0].length; j++) { if (board[i][j] == word.charAt(0)) { if (dfs(board, word, i, j, 0)) { returntrue; } } } } returnfalse; }
publicbooleandfs(char[][] board, String word, int i, int j, int k){ if (k == word.length()) { returntrue; } if (i < 0 || i >= board.length || j < 0 || j >= board[0].length) { returnfalse; } if (board[i][j] != word.charAt(k)) { returnfalse; } char t = board[i][j]; board[i][j] = '0'; boolean res = dfs(board, word, i + 1, j, k + 1) || dfs(board, word, i - 1, j, k + 1) || dfs(board, word, i, j + 1, k + 1) || dfs(board, word, i, j - 1, k + 1); board[i][j] = t; return res; } }
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ classSolution{ public TreeNode buildTree(int[] preorder, int[] inorder){ List<Integer> pre = new ArrayList<>(); for (int i = 0; i < preorder.length; i++) { pre.add(preorder[i]); } List<Integer> in = new ArrayList<>(); for (int i = 0; i < inorder.length; i++) { in.add(inorder[i]); } return func(pre, in); }
public TreeNode func(List<Integer> pre, List<Integer> in){ if (pre.size() == 0) { returnnull; } int val = pre.get(0); TreeNode root = new TreeNode(val); int ans = in.indexOf(val); root.left = func(pre.subList(1, ans + 1), in.subList(0, ans)); root.right = func(pre.subList(ans + 1, pre.size()), in.subList(ans + 1, in.size())); return root; } }
常用算法
长度最小的子数组
给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的 连续 子数组,并返回其长度。如果不存在符合条件的子数组,返回 0。
// 滑动窗口 classSolution{ publicintminSubArrayLen(int s, int[] nums){ int n = nums.length; if (n == 0) { return0; } int ans = Integer.MAX_VALUE; int start = 0, end = 0; int sum = 0; while (end < n) { sum += nums[end]; while (sum >= s) { ans = Math.min(ans, end - start + 1); sum -= nums[start]; start++; } end++; } return ans == Integer.MAX_VALUE ? 0 : ans; } }
// 并查集 publicclassMain{ publicstaticvoidmain(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int p = sc.nextInt(); int[] nums = newint[n + 1]; for (int i = 1; i <= n; i++) { nums[i] = i; } for (int i = 0; i < m; i++) { int x = sc.nextInt(); int y = sc.nextInt(); union(nums, x, y); } for (int i = 0; i < p; i++) { int x = sc.nextInt(); int y = sc.nextInt(); if (find(nums, x, y)) { System.out.println("Yes"); } else { System.out.println("No"); } } }
publicstaticbooleanfind(int[] nums, int x, int y){ return nums[x] == nums[y]; }
publicstaticvoidunion(int[] nums, int x, int y){ int num1 = nums[x]; int num2 = nums[y]; if (num1 != num2) { for (int i = 0; i < nums.length; i++) { if (nums[i] == num1) { nums[i] = num2; } } } } }
classNode{ int val; Node next; Node(int x) { val = x; } }
publicclassSolution{ public Node reverse(Node root){ Node next = null; Node prev = null; while (root != null) { next = root.next; root.next = prev; prev = root; root = next; } return prev; } }
贪心
两地调度
公司计划面试 2N 人。第 i 人飞往 A 市的费用为 costs[i][0],飞往 B 市的费用为 costs[i][1]。
返回将每个人都飞到某座城市的最低费用,要求每个城市都有 N 人抵达。
1 2 3 4 5 6 7 8 9
输入:[[10,20],[30,200],[400,50],[30,20]] 输出:110 解释: 第一个人去 A 市,费用为 10。 第二个人去 A 市,费用为 30。 第三个人去 B 市,费用为 50。 第四个人去 B 市,费用为 20。
最低总费用为 10 + 30 + 50 + 20 = 110,每个城市都有一半的人在面试。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution{ publicinttwoCitySchedCost(int[][] costs){ int res = 0; int[] temp = newint[costs.length]; for (int i = 0; i < costs.length; i++) { temp[i] = costs[i][1] - costs[i][0]; res = res + costs[i][0]; } Arrays.sort(temp); for (int i = 0; i < costs.length / 2; i++) { res = res + temp[i]; } return res; } }
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ classSolution{ public List<String> binaryTreePaths(TreeNode root){ List<String> res = new ArrayList<>(); func(root, "", res); return res; }