leetcode-1339. 分裂二叉树的最大乘积
leetcode-1339. 分裂二叉树的最大乘积
原始思路
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354class Solution { long max = 0; TreeNode r; Map<TreeNode, Long> map = new HashMap<>(); public int maxProduct(TreeNode root) { // 初始化map dfs(root); r = root; //求最大乘积 dfs2(root); return (int)(max % 1000000007); } public void dfs2(TreeNode root) { if (root == null) { ...
leetcode-1347. 制造字母异位词的最小步骤数
leetcode-1347. 制造字母异位词的最小步骤数
原始思路
1234567891011121314151617181920212223242526class Solution { public int minSteps(String s, String t) { Map<Character, Integer> sMap = new HashMap<>(); Map<Character, Integer> tMap = new HashMap<>(); for (int i = 0; i < s.length(); i++) { sMap.put(s.charAt(i), sMap.getOrDefault(s.charAt(i), 0) + 1); tMap.put(t.charAt(i), tMap.getOrDefault(t.charAt(i), 0) + 1); } ...
leetcode-面试题 08.09. 括号
leetcode-面试题 08.09. 括号
1234567891011121314151617181920class Solution { List<String> result = new ArrayList<>(); public List<String> generateParenthesis(int n) { dfs(0, 0, "", n); return result; } public void dfs(int left, int right, String str, int n) { if (str.length() == 2 * n) { result.add(str); } if (left < n) { dfs(left + 1, right, str + "(", n); ...
leetcode-530. 二叉搜索树的最小绝对差
leetcode-530. 二叉搜索树的最小绝对差
原始思路
123456789101112131415161718192021222324252627class Solution { List<Integer> list = new ArrayList<Integer>(); int result = Integer.MAX_VALUE; public int getMinimumDifference(TreeNode root) { dfs(root); for(int i = 1; i < list.size(); i++){ result = Math.min(result,list.get(i)-list.get(i-1)); } return result; } public void dfs(TreeNode root){ if(root == null) ...
leetcode-416. 分割等和子集
leetcode-416. 分割等和子集
0-1背包问题
123456789101112131415161718192021222324class Solution { public boolean canPartition(int[] nums) { int n = nums.length; int total = 0; for(int i = 0; i < nums.length; i++){ total += nums[i]; } if(total % 2 == 1){ //总和为奇数 return false; } int half = total / 2; int[] dp = new int[half+1]; for(int i = 1; i <= n; i++){ for( ...
leetcode-5536. 最大网络秩
leetcode-5536. 最大网络秩
原始思路
123456789101112131415161718192021222324252627282930313233class Solution { public int maximalNetworkRank(int n, int[][] roads) { Map<Integer, Set<Integer>> graph = new HashMap<>(); for(int i = 0; i < n; i++){ graph.put(i, new HashSet<Integer>()); } for(int[] road: roads){ int a = road[0]; int b = road[1]; graph.get(a).add(b); graph.ge ...
leetcode-1249. 移除无效的括号
leetcode-1249. 移除无效的括号
原始思路
12345678910111213141516171819202122232425262728293031class Solution { public String minRemoveToMakeValid(String s) { Stack<Integer> stack = new Stack<>(); char ch1 = '('; char ch2 = ')'; Set<Integer> set = new HashSet<>(); for (int i = 0; i < s.length(); i++) { // 将'('入栈 char ch = s.charAt(i); if (ch == ch1) { ...
leetcode-面试题 17.15. 最长单词
leetcode-面试题 17.15. 最长单词
题解思路
12345678910111213141516171819202122232425262728293031323334class Solution { public String longestWord(String[] words) { Arrays.sort(words,(o1,o2)->{ if(o1.length() == o2.length()) return o1.compareTo(o2); else{ return Integer.compare(o2.length(),o1.length()); } }); Set<String> set = new HashSet<>(Arrays.asList(words)); for (Stri ...
leetcode-1609. 奇偶树
leetcode-1609. 奇偶树
原始思路
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950class Solution { public boolean isEvenOddTree(TreeNode root) { LinkedList<TreeNode> queue = new LinkedList<TreeNode>(); queue.offer(root); boolean b = true; int level = 0; while (!queue.isEmpty()){ int n = queue.size(); int tmp = Integer.MAX_VALUE; int tmp2 = Integer.MIN_VALUE; ...
leetcode-142. 环形链表 II
leetcode-142. 环形链表 II
原始思路
12345678910111213141516public class Solution { Set<ListNode> set = new HashSet<>(); public ListNode detectCycle(ListNode head) { if (head == null) { return null; } if (set.contains(head)){ return head; } set.add(head); if (head.next!=null){ return detectCycle(head.next); } return null; }}