This commit is contained in:
whai 2024-03-31 11:34:29 +08:00
parent 2cfc6687bf
commit c4248f2cfe
17 changed files with 1093 additions and 2 deletions

View File

@ -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<String> 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<String> 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<Character, Integer> map = new HashMap<>();
backTacking(chars, 0, map);
System.out.println(res);
}
public static void backTacking(char[] chars, int start, Map<Character,Integer> 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<Character, Integer> 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<Integer> map = new ArrayList<>();
// backTacking(chars, 0, map);
// System.out.println(res);
// }
//
// public static void backTacking(char[] chars, int start, ArrayList<Integer> 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<Character, Integer> 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
}
}
}

View File

@ -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<Integer> 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{

View File

@ -90,6 +90,7 @@ public class LeetCode202 {
Set<Integer> set = new HashSet<>();
while (n != 1) {
n = getNextNumber(n);
System.out.println(n);
if (set.contains(n)) {
return false;
} else {

View File

@ -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)

View File

@ -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<List<Integer>> lists = solution.threeSum(nums);
System.out.println(lists);
}
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> 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;
}
}
}

View File

@ -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();
}
}
}

View File

@ -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<List<Integer>> lists = solution.fourSum(nums, target);
System.out.println(lists);
}
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
List<List<Integer>> 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;
}
}
}

View File

@ -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<Integer> 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;
}
}
}

View File

@ -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;
}
}
}

View File

@ -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<Integer> queue1;
Deque<Integer> 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();
*/
}

View File

@ -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<Integer> stack1;
Deque<Integer> 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();
*/
}

View File

@ -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);
}
}
}

View File

@ -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;
}
}
}

View File

@ -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<Integer> 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;
}
}
}

View File

@ -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<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
}
PriorityQueue<Integer> 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;
}
}
}

View File

@ -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<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
circle(root, list);
return list;
}
public void circle(TreeNode treeNode, List<Integer> list) {
if (treeNode == null) {
return;
}
circle(treeNode.left, list);
list.add(treeNode.val);
circle(treeNode.right, list);
}
public List<Integer> inorderTraversal1(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root == null) {
return list;
}
Deque<TreeNode> 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<Integer> inorderTraversal2(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root == null) {
return list;
}
Deque<TreeNode> 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<Integer> inorderTraversal3(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root == null) {
return list;
}
Deque<TreeNode> 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<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) {
return res;
}
Deque<TreeNode> deque = new LinkedList();
deque.add(root);
while (!deque.isEmpty()) {
int size = deque.size();
List<Integer> 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;
}
}
}

4
目录.md Normal file
View File

@ -0,0 +1,4 @@
# 目录
- [main](#main)
- [test](#test)