diff --git a/src/main/java/cn/whaifree/interview/Meituan/MT3_39.java b/src/main/java/cn/whaifree/interview/Meituan/MT3_39.java new file mode 100644 index 0000000..9ce0252 --- /dev/null +++ b/src/main/java/cn/whaifree/interview/Meituan/MT3_39.java @@ -0,0 +1,232 @@ +package cn.whaifree.interview.Meituan; + +import java.util.*; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/30 19:09 + * @注释 + */ +public class MT3_39 { + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int k = scanner.nextInt(); + int x = scanner.nextInt(); + int y = scanner.nextInt(); + // xz + x = tl + // yd - y = xz + // xz + yd + tl = k + int tl = (k - 2 * x - y) / 3; + int xz = tl + x; + int yd = xz + y; + System.out.print(tl + " " + yd + " " + xz); + + } + + static class p2{ + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + int[] nums = new int[n]; + int max = Integer.MIN_VALUE; + for (int i = 0; i < n; i++) { + nums[i] = scanner.nextInt(); + max = Math.max(max, nums[i]); + } + + + for (int num : nums) { + if (num * 2 > max) { + System.out.print(num * 2); + System.out.print(" "); + } else { + System.out.print(max); + System.out.print(" "); + } + } + } + } + + static class p4{ + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + String s1 = scanner.next(); + String s2 = scanner.next(); + char[] c1 = s1.toCharArray(); + char[] c2 = s2.toCharArray(); + // 遇到不一样的就对前面全部不一样的操作 + int index = s1.length()-1; + + + int flag = 0; + for (int i = 0; i < c1.length; i++) { + if (c1[i] == c1[i + 1] && c2[i] != c2[i + 1]) { + flag = 0; + break; + } else if (c1[i] != c1[i + 1] && c2[i] == c2[i + 1]) { + flag = 1; + break; + } else if (c1[i] != c1[i + 1] && c2[i] != c2[i + 1]) { + flag = 2; + break; + } + } + + if (flag == 0) { + List list = new ArrayList<>(); + while (index >= 0) { + if (c1[index]!= c2[index]) { + for (int i = index; i >= 0; i--) { + c2[i] = c1[index]; + } + list.add("2 " + (index + 1) + " " + c1[index]); + } + index--; + } + System.out.println(list.size()); + list.forEach(System.out::println); + } else { + List list2 = new ArrayList<>(); + index = s1.length() - 1; + c1 = s1.toCharArray(); + c2 = s2.toCharArray(); + while (index >= 0) { + if (c2[index]!= c1[index]) { + for (int i = index; i >= 0; i--) { + c1[i] = c2[index]; + } + list2.add("1 " + (index + 1) + " " + c2[index]); + } + index--; + } + System.out.println(list2.size()); + list2.forEach(System.out::println); + } + + + + } + + } + + static class p5{ + static long res = 0; + public static void main(String[] args) { + + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + String s = scanner.next(); + char[] chars = s.toCharArray(); + + HashMap map = new HashMap<>(); + backTacking(chars, 0, map); + System.out.println(res); + } + + public static void backTacking(char[] chars, int start, Map map) { + if (map.size() == 2 && isEqualValue(map)) { + res++; + } + if (start > chars.length || map.size() > 2) { + return; + } + + for (int i = start; i < chars.length; i++) { + map.put(chars[i], map.getOrDefault(chars[i], 0) + 1); + backTacking(chars, i + 1, map); + Integer integer = map.get(chars[i]); + if (integer == 1) { + map.remove(chars[i]); + }else { + map.put(chars[i], map.get(chars[i]) - 1); + } + } + } + + public static boolean isEqualValue(Map map) { + // 判断map的所有value是否一样 + for (Integer value : map.values()) { + if (value != map.values().iterator().next()) { + return false; + } + } + return true; + } + } + +// static class p6{ +// static int res = 0; +// public static void main(String[] args) { +// +// Scanner scanner = new Scanner(System.in); +// int n = scanner.nextInt(); +// String s = scanner.next(); +// char[] chars = s.toCharArray(); +// +// ArrayList map = new ArrayList<>(); +// backTacking(chars, 0, map); +// System.out.println(res); +// } +// +// public static void backTacking(char[] chars, int start, ArrayList map) { +// if (map.size() == 2 && isEqualValue(map)) { +// res++; +// } +// if (start > chars.length || map.size() > 2) { +// return; +// } +// +// for (int i = start; i < chars.length; i++) { +// map; +// backTacking(chars, i + 1, map); +// Integer integer = map.get(chars[i]); +// if (integer == 1) { +// map.remove(chars[i]); +// }else { +// map.put(chars[i], map.get(chars[i]) - 1); +// } +// } +// } +// +// public static boolean isEqualValue(Map map) { +// // 判断map的所有value是否一样 +// for (Integer value : map.values()) { +// if (value != map.values().iterator().next()) { +// return false; +// } +// } +// return true; +// } +// } + + static class p7{ + + /** + * 小美有n个朋友,她准备请其中一些朋友来吃饭。 + * 其中有一些朋友有暗恋关系:假设a暗恋b,那么小美带上b的时候a也必须去,否则a就会不开心。 + * 小美想知道,一共有多少种不同的请客方案可以让每个人都开心?由于答案可能过大,请对10^9+7取模。 + * @param args + */ + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + int peopleNumber = scanner.nextInt(); + int relation = scanner.nextInt(); + int[] rela = new int[peopleNumber + 1]; + for (int i = 0; i < relation; i++) { + int from = scanner.nextInt(); + int to = scanner.nextInt(); + rela[from] = to; + } + // rela 表示 index的人暗恋 里面值的那个 + // 1 2 + // 2 3 + // 3 4 + + + + + } + } +} diff --git a/src/main/java/cn/whaifree/interview/XieChen/xc328.java b/src/main/java/cn/whaifree/interview/XieChen/xc328.java index 86519f8..0812136 100644 --- a/src/main/java/cn/whaifree/interview/XieChen/xc328.java +++ b/src/main/java/cn/whaifree/interview/XieChen/xc328.java @@ -1,5 +1,6 @@ package cn.whaifree.interview.XieChen; +import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; @@ -77,8 +78,12 @@ public class xc328 { } - // 将偶数的区间/2 让整个区间最大 - public static void main(String[] args) { + /** + * 将偶数的区间/2 让整个区间最大 + * 给定一个数组,对一个区间(区间内全部都是偶数)全部/2,使得整个数组和最小 + * @param args + */ + public static void main1(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); long[] nums = new long[n]; @@ -129,6 +134,89 @@ public class xc328 { } System.out.println(sum - minSum / 2); } + + /** + * 2 -2 4 -8 5 + * @param args + */ + + public static void main(String[] args) { + String s = new String("abc") + new String("cde"); + + Scanner in = new Scanner(System.in); + int n = in.nextInt(); + long[] nums = new long[n]; + for (int i = 0; i < n; i++) { + nums[i] = in.nextLong(); + } + // 获取前缀和 + long[] preSum = new long[n + 1]; + for (int i = 1; i < n + 1; i++) { + preSum[i] = preSum[i - 1] + nums[i - 1]; + } + // 找到最小区间 + // 用求最大连续子串和的思想求出array中连续的最小的负值 + // 2 -4 -2 8 1 + + // 遇到区间总和>0的直接跳过 + long nowSum = 0; + long minSum = Integer.MAX_VALUE; + // 滑动窗口,让窗口内的值都为偶数,并且是总和最小的窗口 + + for (int i = 0; i < nums.length; i++) { + for (int j = i; j < nums.length; j++) { + if (Math.abs(nums[j] % 2) == 1) { + break; + } + nowSum += nums[j]; + minSum = Math.min(nowSum, minSum); + if (nowSum > 0) { + // 最大连续子串和的思想: + // 我们要找到最小的负数区间(即负的越多越好), + // 如果加上某个区间后,这个区间的值为整数,必然这个区间是会让整体区间变大,所以直接不取这个区间 + break; + } + } + nowSum = 0; + } + long sum = 0; + for (long num : nums) { + sum += num; + } + System.out.println(sum - minSum / 2); + + } + + public static void main43(String[] args) { + Scanner scanner = new Scanner(System.in); + int n = scanner.nextInt(); + ArrayList nums = new ArrayList<>(); + long ans = 0; + for (int i = 0; i < n; ++i) { + int t = scanner.nextInt(); + nums.add(t); + ans += t; + } + int left = 0, right = 0; + long tsum = 0; + long minum = 0; + + while (right < n) { + if ((nums.get(right) & 1) == 1 || tsum + nums.get(right) >= 0) { + right++; + left = right; + tsum = 0; + } else { + tsum += nums.get(right); + minum = Math.min(minum, tsum); + right++; + } + // System.out.println(tsum); + } + + System.out.println(ans - (minum / 2)); + } + } static class p4{ diff --git a/src/main/java/cn/whaifree/leetCode/Hash/LeetCode202.java b/src/main/java/cn/whaifree/leetCode/Hash/LeetCode202.java index e3a3516..cf32fd2 100644 --- a/src/main/java/cn/whaifree/leetCode/Hash/LeetCode202.java +++ b/src/main/java/cn/whaifree/leetCode/Hash/LeetCode202.java @@ -90,6 +90,7 @@ public class LeetCode202 { Set set = new HashSet<>(); while (n != 1) { n = getNextNumber(n); + System.out.println(n); if (set.contains(n)) { return false; } else { diff --git a/src/main/java/cn/whaifree/redo/redoAll/2024_3_30.md b/src/main/java/cn/whaifree/redo/redoAll/2024_3_30.md new file mode 100644 index 0000000..4b8930e --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/2024_3_30.md @@ -0,0 +1,9 @@ +[LeetCode206.java](LeetCode206.java) +[LeetCode18.java](LeetCode18.java) +[LeetCode209.java](LeetCode209.java) +[LeetCode202.java](LeetCode202.java) +[LeetCode15.java](LeetCode15.java) +水平遍历 [LeetCode94_145_144_102.java](LeetCode94_145_144_102.java) + + + diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode15.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode15.java new file mode 100644 index 0000000..32dafa7 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode15.java @@ -0,0 +1,63 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/30 15:35 + * @注释 + */ +public class LeetCode15 { + + @Test + public void test() + { + Solution solution = new Solution(); + int[] nums = new int[]{-2,0,0,2,2}; + List> lists = solution.threeSum(nums); + System.out.println(lists); + } + + class Solution { + public List> threeSum(int[] nums) { + + List> res = new ArrayList<>(); + Arrays.sort(nums); + + for (int i = 0; i < nums.length; i++) { + + if (i > 0 && nums[i] == nums[i - 1]) { + continue; + } + + int left = i + 1; + int right = nums.length - 1; + while (left < right) { + int sum = nums[i] + nums[left] + nums[right]; + if (sum == 0) { + res.add(Arrays.asList(nums[i], nums[left], nums[right])); + while (left < right && nums[left] == nums[left + 1]) { + left++; + } + while (left < right && nums[right] == nums[right - 1]) { + right--; + } + left++; + right--; + } else if (sum > 0) { + right--; + }else { + left++; + } + } + } + return res; + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode151.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode151.java new file mode 100644 index 0000000..6118ba9 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode151.java @@ -0,0 +1,35 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/30 13:51 + * @注释 + */ +public class LeetCode151 { + @Test + public void test() + { + Solution solution = new Solution(); + String s = " hello "; + String s1 = solution.reverseWords(s); + System.out.println(s1); + } + + class Solution { + public String reverseWords(String s) { + // s = " hello world " + // 去除前后空白串 + s = s.trim(); + String[] split = s.split("\\s+"); // 活着 " +" + StringBuilder stringBuilder = new StringBuilder(); + for (int i = split.length - 1; i > 0; i--) { + stringBuilder.append(split[i]).append(" "); + } + stringBuilder.append(split[0]); + return stringBuilder.toString(); + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode18.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode18.java new file mode 100644 index 0000000..91093d4 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode18.java @@ -0,0 +1,76 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/30 12:46 + * @注释 + */ +public class LeetCode18 { + + @Test + public void test() + { + int[] nums = {-1,0,-5,-2,-2,-4,0,1,-2}; + int target = -9; + Solution solution = new Solution(); + List> lists = solution.fourSum(nums, target); + System.out.println(lists); + } + + class Solution { + public List> fourSum(int[] nums, int target) { + + Arrays.sort(nums); + List> res = new ArrayList<>(); + + if (nums[0] > target && nums[0] > 0) { + return res; + } + + for (int i = 0; i < nums.length; i++) { + // 重复去除 + if (i > 0 && nums[i] == nums[i - 1]) { + continue; + } + + for (int j = i + 1; j < nums.length; j++) { + // 第一次给过 + if (j > i + 1 && nums[j] == nums[j - 1]) { + continue; + } + + int left = j + 1; + int right = nums.length - 1; + while (left < right) { + int sum = nums[i] + nums[j] + nums[left] + nums[right]; + if (sum == target) { + res.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right])); + while (right > left && nums[left] == nums[left+ 1]) { + left++; + } + while (right > left && nums[right] == nums[right - 1]) { + right--; + } + left++; + right--; + } else if (sum > target) { + right--; + }else { + left++; + } + + } + } + } + return res; + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode202.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode202.java new file mode 100644 index 0000000..102acf9 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode202.java @@ -0,0 +1,44 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +import java.util.HashSet; +import java.util.Set; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/30 15:29 + * @注释 + */ +public class LeetCode202 { + + @Test + public void test() { + Solution solution = new Solution(); + boolean happy = solution.isHappy(2); + System.out.println(happy); + } + + class Solution { + public boolean isHappy(int n) { + Set set = new HashSet<>(); + while (n > 1) { + if (set.contains(n)) return false; + set.add(n); + n = get(n); + } + return true; + } + + public int get(int n) { + int res = 0; + while (n > 0) { + int retail = n % 10; + res += retail * retail; + n /= 10; + } + return res; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode209.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode209.java new file mode 100644 index 0000000..4bf2675 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode209.java @@ -0,0 +1,42 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/30 14:53 + * @注释 + */ +public class LeetCode209 { + + @Test + public void test() { + int[] nums = {1,2,3,4,5}; + int target = 11; + int minSubArrayLen = new Solution().minSubArrayLen(target, nums); + System.out.println(minSubArrayLen); + } + + class Solution { + public int minSubArrayLen(int target, int[] nums) { + + int nowSum = 0; + int left = 0; + int right = 0; + int minLength = Integer.MAX_VALUE; + + while (right < nums.length) { + nowSum += nums[right]; + while (nowSum >= target) { + minLength = Math.min(right - left + 1, minLength); + nowSum -= nums[left]; + left++; + } + right++; + } + + return minLength == Integer.MAX_VALUE ? 0 : minLength; + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode225.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode225.java new file mode 100644 index 0000000..c43b3b4 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode225.java @@ -0,0 +1,73 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +import java.util.Deque; +import java.util.LinkedList; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/30 16:11 + * @注释 + */ +public class LeetCode225 { + + @Test + public void test() + { + MyStack myStack = new MyStack(); + myStack.push(1); + myStack.push(2); + myStack.push(3); + myStack.push(4); + myStack.push(5); + System.out.println(myStack.pop()); + System.out.println(myStack.pop()); + System.out.println(myStack.pop()); + System.out.println(myStack.top()); + } + + class MyStack { + + Deque queue1; + Deque queue2; + + public MyStack() { + queue1 = new LinkedList<>(); + queue2 = new LinkedList<>(); + } + + public void push(int x) { + queue1.add(x); + } + + public int pop() { + while (queue1.size() != 1) { + queue2.add(queue1.pop()); + } + Integer pop = queue1.pop(); + while (!queue2.isEmpty()) { + queue1.add(queue2.pop()); + } + return pop; + } + + public int top() { + return queue1.peekLast(); + } + + public boolean empty() { + return queue1.isEmpty(); + } + } + +/** + * Your MyStack object will be instantiated and called as such: + * MyStack obj = new MyStack(); + * obj.push(x); + * int param_2 = obj.pop(); + * int param_3 = obj.top(); + * boolean param_4 = obj.empty(); + */ +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode232.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode232.java new file mode 100644 index 0000000..6f37a70 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode232.java @@ -0,0 +1,68 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +import java.util.ArrayDeque; +import java.util.Deque; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/30 16:23 + * @注释 + */ +public class LeetCode232 { + + @Test + public void test() { + MyQueue myQueue = new MyQueue(); + myQueue.push(1); // queue is: [1] + myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue) + System.out.println(myQueue.peek()); // return 1 + System.out.println(myQueue.pop()); // return 1, queue is [2] + System.out.println(myQueue.empty()); // return false + } + + class MyQueue { + + Deque stack1; + Deque stack2; + + public MyQueue() { + stack1 = new ArrayDeque<>(); + stack2 = new ArrayDeque<>(); + } + + public void push(int x) { + stack1.push(x); + } + + public int pop() { + while (!stack1.isEmpty()) { + stack2.push(stack1.pop()); + } + Integer pop = stack2.pop(); + while (!stack2.isEmpty()) { + stack1.push(stack2.pop()); + } + return pop; + } + + public int peek() { + return stack1.peekLast(); + } + + public boolean empty() { + return stack1.isEmpty(); + } + } + +/** + * Your MyQueue object will be instantiated and called as such: + * MyQueue obj = new MyQueue(); + * obj.push(x); + * int param_2 = obj.pop(); + * int param_3 = obj.peek(); + * boolean param_4 = obj.empty(); + */ +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode2487.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode2487.java new file mode 100644 index 0000000..4056125 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode2487.java @@ -0,0 +1,54 @@ +package cn.whaifree.redo.redoAll; + +import cn.whaifree.leetCode.model.ListNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/30 12:31 + * @注释 + */ +public class LeetCode2487 { + + @Test + public void test() { + new Solution().removeNodes(ListNode.listNodeFromArray(new int[]{5})).printList(); + } + + class Solution { + /** + * 移除每个右侧有一个更大数值的节点。 + * 逆转后,移除左侧更小的节点 + * @param head + * @return + */ + public ListNode removeNodes(ListNode head) { + ListNode reverse = reverse(head); + ListNode index = reverse; + while (index.next != null) { + if (index.next.val < index.val) { + index.next = index.next.next; + }else { + index = index.next; + } + } + + return reverse(reverse); + } + + public ListNode reverse(ListNode head) { + return rev(null, head); + } + + public ListNode rev(ListNode before, ListNode after) { + if (after == null) { + return before; + } + ListNode next = after.next; + after.next = before; + return rev(after, next); + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode28.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode28.java new file mode 100644 index 0000000..723a2eb --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode28.java @@ -0,0 +1,32 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/30 13:41 + * @注释 + */ +public class LeetCode28 { + + @Test + public void test() { + Solution solution = new Solution(); + int i = solution.strStr("ab", "a"); + System.out.println(i); + } + + class Solution { + public int strStr(String haystack, String needle) { + int length = needle.length(); + for (int i = 0; i < haystack.length() - length + 1; i++) { + if (haystack.substring(i, i + length).equals(needle)) { + return i; + } + } + return -1; + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode287.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode287.java new file mode 100644 index 0000000..d045c68 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode287.java @@ -0,0 +1,71 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +import java.util.HashSet; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/30 11:40 + * @注释 + */ +public class LeetCode287 { + + @Test + public void test() + { + + int[] nums = {3,1,3,4,2}; + int i = new Solution1().findDuplicate(nums); + System.out.println(i); + } + + class Solution { + public int findDuplicate(int[] nums) { + HashSet map = new HashSet<>(); + for (int num : nums) { + if (map.contains(num)) { + return num; + }else { + map.add(num); + } + } + return -1; + } + } + + class Solution1 { + /** + * 循环链表的入口 + * 0 1 2 3 4 + * 3 1 3 4 2 + * + * 0 3 4 2 3 + * + * 0-->1--->3---->2--->4 + * |----| + * + * @param nums + * @return + */ + public int findDuplicate(int[] nums) { + int fast = 0; + int slow = 0; + do { + fast = nums[nums[fast]]; + slow = nums[slow]; + } + while (fast != slow); + // 此时找到交会点 + + fast = 0; + while (fast != slow) { + fast = nums[fast]; + slow = nums[slow]; + } + return fast; + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode347.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode347.java new file mode 100644 index 0000000..3c48545 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode347.java @@ -0,0 +1,53 @@ +package cn.whaifree.redo.redoAll; + +import org.junit.Test; + +import java.util.HashMap; +import java.util.PriorityQueue; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/30 16:54 + * @注释 + */ +public class LeetCode347 { + + @Test + public void test() + { + Solution solution = new Solution(); + int[] nums = {1,1,1,2,2,3}; + int k = 2; + int[] res = solution.topKFrequent(nums, k); + for (int i = 0; i < res.length; i++) { + System.out.println(res[i]); + } + } + + class Solution { + public int[] topKFrequent(int[] nums, int k) { + // 频率统计 + HashMap map = new HashMap<>(); + for (int i = 0; i < nums.length; i++) { + map.put(nums[i], map.getOrDefault(nums[i], 0) + 1); + } + + PriorityQueue queue = new PriorityQueue<>( + k, (o1, o2) -> map.get(o2) - map.get(o1) + ); + + for (Integer integer : map.keySet()) { + queue.add(integer); + } + + int[] res = new int[k]; + for (int i = 0; i < res.length; i++) { + res[i] = queue.poll(); + } + return res; + + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redoAll/LeetCode94_145_144_102.java b/src/main/java/cn/whaifree/redo/redoAll/LeetCode94_145_144_102.java new file mode 100644 index 0000000..d0f616b --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redoAll/LeetCode94_145_144_102.java @@ -0,0 +1,146 @@ +package cn.whaifree.redo.redoAll; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Deque; +import java.util.LinkedList; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/30 17:48 + * @注释 + */ +public class LeetCode94_145_144_102 { + + @Test + public void test( + ) { + new Solution().levelOrder(TreeNode.constructTreeByArray(1,2,3,4,5,6)).forEach( + System.out::println + ); + } + + class Solution { + public List inorderTraversal(TreeNode root) { + List list = new ArrayList<>(); + circle(root, list); + return list; + } + + public void circle(TreeNode treeNode, List list) { + if (treeNode == null) { + return; + } + circle(treeNode.left, list); + list.add(treeNode.val); + circle(treeNode.right, list); + } + + public List inorderTraversal1(TreeNode root) { + + List list = new ArrayList<>(); + if (root == null) { + return list; + } + Deque stack = new LinkedList<>(); + + stack.push(root); + while (!stack.isEmpty()) { + TreeNode pop = stack.pop(); + if (pop == null) { + TreeNode index = stack.pop(); + list.add(index.val); + }else { + // 第一次进入,加入null标签 + if (pop.right!=null) stack.push(pop.right); + stack.push(pop); + stack.push(null); + if (pop.left!=null) stack.push(pop.left); + } + } + + return list; + } + + + // 后序遍历 + public List inorderTraversal2(TreeNode root) { + + List list = new ArrayList<>(); + if (root == null) { + return list; + } + Deque stack = new LinkedList<>(); + + stack.push(root); + while (!stack.isEmpty()) { + TreeNode pop = stack.pop(); + if (pop == null) { + TreeNode index = stack.pop(); + list.add(index.val); + }else { + // 第一次进入,加入null标签 + stack.push(pop); + stack.push(null); + if (pop.right!=null) stack.push(pop.right); + if (pop.left!=null) stack.push(pop.left); + } + } + + return list; + } + + + public List inorderTraversal3(TreeNode root) { + + List list = new ArrayList<>(); + if (root == null) { + return list; + } + Deque stack = new LinkedList<>(); + + stack.push(root); + while (!stack.isEmpty()) { + TreeNode pop = stack.pop(); + list.add(pop.val); + if (pop.right != null) { + stack.push(pop.right); + } + if (pop.left != null) { + stack.push(pop.left); + } + } + + return list; + } + + + + public List> levelOrder(TreeNode root) { + List> res = new ArrayList<>(); + + if (root == null) { + return res; + } + Deque deque = new LinkedList(); + deque.add(root); + while (!deque.isEmpty()) { + int size = deque.size(); + List e = new ArrayList<>(); + for (int i = 0; i < size; i++) { + TreeNode pop = deque.pop(); + e.add(pop.val); + if (pop.left!=null) deque.add(pop.left); + if (pop.right!=null) deque.add(pop.right); + } + res.add(e); + } + return res; + } + } + +} diff --git a/目录.md b/目录.md new file mode 100644 index 0000000..9e1ffa6 --- /dev/null +++ b/目录.md @@ -0,0 +1,4 @@ +# 目录 + +- [main](#main) +- [test](#test)