From bbb29153f535b64aa546908d2945149b5d922cd6 Mon Sep 17 00:00:00 2001 From: whai Date: Sun, 3 Mar 2024 20:09:37 +0800 Subject: [PATCH] redo --- .../cn/whaifree/leetCode/StaticObjTest.java | 21 +++ src/main/java/cn/whaifree/leetCode/T.java | 130 ------------------ .../redo/redo_24_3_1/LeetCode1005.java | 77 +++++++++++ .../redo/redo_24_3_1/LeetCode134.java | 5 + .../redo/redo_24_3_1/LeetCode135.java | 49 +++++++ .../redo/redo_24_3_1/LeetCode406.java | 80 +++++++++++ .../redo/redo_24_3_1/LeetCode491.java | 56 ++++++++ .../whaifree/redo/redo_24_3_1/LeetCode51.java | 2 +- .../redo/redo_24_3_1/LeetCode860.java | 45 ++++++ 9 files changed, 334 insertions(+), 131 deletions(-) create mode 100644 src/main/java/cn/whaifree/leetCode/StaticObjTest.java delete mode 100644 src/main/java/cn/whaifree/leetCode/T.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode1005.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode135.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode406.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode491.java create mode 100644 src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode860.java diff --git a/src/main/java/cn/whaifree/leetCode/StaticObjTest.java b/src/main/java/cn/whaifree/leetCode/StaticObjTest.java new file mode 100644 index 0000000..bb7ae31 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/StaticObjTest.java @@ -0,0 +1,21 @@ +package cn.whaifree.leetCode; + +public class StaticObjTest { + static class Test{ + // 静态变量存方法区 + static ObjectHolder staticObj = new ObjectHolder(); + // 成员变量存堆中 + ObjectHolder instanceObj = new ObjectHolder(); + + void foo(){ + // 变量名存放在foo()方法中的栈帧中的局部变量表中 + ObjectHolder localObj = new ObjectHolder(); + System.out.println("done"); + } + } + + + private static class ObjectHolder{ + + } +} diff --git a/src/main/java/cn/whaifree/leetCode/T.java b/src/main/java/cn/whaifree/leetCode/T.java deleted file mode 100644 index e2af711..0000000 --- a/src/main/java/cn/whaifree/leetCode/T.java +++ /dev/null @@ -1,130 +0,0 @@ -package cn.whaifree.leetCode; - -import java.util.ArrayList; -import java.util.Random; - -/** - * @version 1.0 - * @Author whai文海 - * @Date 2024/2/27 17:58 - * @注释 - */ -public class T { - - public int A() { - B(); - return 1; - } - - public void B() { - - } -} - -interface Animal { - void eat(); -} - -class Hunter{ - void hunt() { - - } -} - -class Cat extends Hunter implements Animal { - - @Override - void hunt() { - System.out.println("cat hunt"); - } - - @Override - public void eat() { - System.out.println("cat eat"); - } -} - -class Dog extends Hunter implements Animal { - - public Dog() { - super(); - } - - public Dog(String name) { - super(); - } - - @Override - void hunt() { - System.out.println("dog hunt"); - } - - @Override - public void eat() { - System.out.println("dog eat"); - } -} - -class AnimalTest { - public static void main(String[] args) { - // 晚期绑定,动态链接 - // 运行时才能确定 - Animal animal = new Cat(); - animal.eat(); - Hunter hunter = (Hunter) animal; - hunter.hunt(); - - // 静态多态 - Dog dog = new Dog("!23"); - - - } -} - - -class Father{ - public static void print(String str){ - System. out. println("father "+str); - } - public void show(String str){ - System. out. println("father"+str); - } -} - -class Son extends Father { - - -} - -class VirtualMethodTest { - public static void main(String[] args) { - Son.print("coder"); - Father fa = new Father(); - fa.show("a"); - } -} - - - -class Picture{ - int number = 0; - public Picture() { - - } - - public Picture(int number) { - this.number = number; - } - - public static void main(String[]args){ - ArrayList list = new ArrayList<>(); - while(true){ - try { - Thread.sleep(10); - } catch (InterruptedException e){ - e.printStackTrace(); - } - list.add(new Picture(new Random().nextInt(1024*1024))); - } - } -} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode1005.java b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode1005.java new file mode 100644 index 0000000..49a5654 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode1005.java @@ -0,0 +1,77 @@ +package cn.whaifree.redo.redo_24_3_1; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.function.ToIntFunction; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/2 11:40 + * @注释 + */ +public class LeetCode1005 { + + @Test + public void test() { + int[] nums = new int[]{5,6,9,-3,3}; + int k =2; + int res = new Solution1().largestSumAfterKNegations(nums, k); + System.out.println(res); + } + + class Solution { + public int largestSumAfterKNegations(int[] nums, int k) { + // 从小到大排序,把前面的负数替换后,如果k是否有剩余 + Arrays.sort(nums); + int sum = 0; + int minInNums = Integer.MAX_VALUE; + for (int i = 0; i < nums.length; i++) { + if (nums[i] < 0 && k > 0) { + nums[i] = -nums[i]; + k--; + } + minInNums = Math.min(minInNums, nums[i]); + sum += nums[i]; + } + + if ((k % 2 == 1 )) { + sum = sum - minInNums - minInNums; + } + return sum; + + } + } + + class Solution1 { + public int largestSumAfterKNegations(int[] nums, int k) { + // 按照绝对值排序 + nums = Arrays.stream(nums).boxed().sorted(new Comparator() { + @Override + public int compare(Integer o1, Integer o2) { + return Math.abs(o2) - Math.abs(o1); + } + }).mapToInt(Integer::intValue).toArray(); + + + int sum = 0; + // 9 -8 -7 6 4 3 + for (int i = 0; i < nums.length; i++) { + if (nums[i] < 0 && k > 0) { + nums[i] = -nums[i]; + k--; + } + sum += nums[i]; + } + + if (k % 2 == 1 ) { + sum -= nums[nums.length - 1] + nums[nums.length - 1]; + } + return sum; + + } + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode134.java b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode134.java index 3579f08..f28eddd 100644 --- a/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode134.java +++ b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode134.java @@ -10,6 +10,11 @@ import org.junit.Test; */ public class LeetCode134 { + + public static void main(String[] args) { + + } + @Test public void test() { System.out.println(new Solution().canCompleteCircuit(new int[]{1, 2, 3, 4, 5}, new int[]{3, 4, 5, 1, 2})); diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode135.java b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode135.java new file mode 100644 index 0000000..6fce19d --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode135.java @@ -0,0 +1,49 @@ +package cn.whaifree.redo.redo_24_3_1; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/3 12:16 + * @注释 + */ +public class LeetCode135 { + + @Test + public void test() { + new Solution().candy(new int[]{1, 0, 2, 4, 1}); + } + + class Solution { + public int candy(int[] ratings) { + // 从前往后,如果rating高,就+1糖果 + int[] distribute = new int[ratings.length]; + + distribute[0] = 1; + for (int i = 1; i < ratings.length; i++) { + + if (distribute[i]==0) distribute[i] = 1; + if (ratings[i] > ratings[i - 1]) { + distribute[i] = distribute[i - 1] + 1; + } + } + + for (int i = distribute.length - 1; i > 0; i--) { + // 需要同时满足 比两边都高 + if (ratings[i - 1] > ratings[i]) { + distribute[i - 1] = Math.max(distribute[i - 1], distribute[i] + 1); + } + } + + int sum = 0; + for (int i : distribute) { + sum += i; + } + return sum; + } + + } +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode406.java b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode406.java new file mode 100644 index 0000000..af3b65d --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode406.java @@ -0,0 +1,80 @@ +package cn.whaifree.redo.redo_24_3_1; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.LinkedList; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/3 12:30 + * @注释 + */ +public class LeetCode406 { + + + + + @Test + public void test() { + String a = "d"; + String b = "d"; + System.out.println(a == b); + a = new String("d"); + System.out.println("a=" + a); + System.out.println("b=" + b); + System.out.println(a == b); + + new Solution().reconstructQueue( + new int[][]{{7, 0}, {4, 4}, {7, 1}, {5, 0}, {6, 1}, {5, 2}} + ); + } + + class Solution { + public int[][] reconstructQueue(int[][] people) { + + LinkedList e = new LinkedList<>(); + // 1. 安升高先排序,同身高k多的放后面 + Arrays.sort(people, new Comparator() { + @Override + public int compare(int[] o1, int[] o2) { + if (o1[0] == o2[0]) { + return o1[1] - o2[1]; + }else { + return o2[0] - o1[0]; + } + } + }); + + + for (int[] person : people) { + e.add(person[1], person); + } + + + return e.toArray(new int[people.length][]); + } + } + +} + + +class StringExer{ + String str = new String("good"); + char[] chars = {'a', 'b', 'c', 'd'}; + + public void change(String str, char[] chars) { + str = "bad"; + chars[0] = 'e'; + } + + public static void main(String[] args) { + StringExer stringExer = new StringExer(); + stringExer.change(stringExer.str, stringExer.chars); + System.out.println(stringExer.str); + System.out.println(stringExer.chars); + } + +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode491.java b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode491.java new file mode 100644 index 0000000..dac8e6e --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode491.java @@ -0,0 +1,56 @@ +package cn.whaifree.redo.redo_24_3_1; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/3 19:40 + * @注释 + */ +public class LeetCode491 { + + @Test + public void test() { + for (List subsequence : new Solution().findSubsequences(new int[]{4,4,3,2,1})) { + System.out.println(subsequence); + } + } + + class Solution { + List> res = new ArrayList<>(); + LinkedList path = new LinkedList<>(); + + public List> findSubsequences(int[] nums) { + backTracking(nums, 0); + return res; + } + + public void backTracking(int[] nums, int start) { + if (path.size() >= 2) { + res.add(new LinkedList<>(path)); + } + if (start >= nums.length) { + return; + } + + HashSet set = new HashSet<>(); + for (int i = start; i < nums.length; i++) { + + if ((path.isEmpty() || path.getLast() <= nums[i]) && !set.contains(nums[i])) { + path.add(nums[i]); + set.add(nums[i]); + backTracking(nums, i + 1); + path.removeLast(); + } + } + + } + } + +} diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode51.java b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode51.java index 17bcb01..e349252 100644 --- a/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode51.java +++ b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode51.java @@ -1,6 +1,6 @@ package cn.whaifree.redo.redo_24_3_1; -import cn.whaifree.leetCode.T; + import org.junit.Test; import java.util.ArrayList; diff --git a/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode860.java b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode860.java new file mode 100644 index 0000000..9494239 --- /dev/null +++ b/src/main/java/cn/whaifree/redo/redo_24_3_1/LeetCode860.java @@ -0,0 +1,45 @@ +package cn.whaifree.redo.redo_24_3_1; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/3/3 11:57 + * @注释 + */ +public class LeetCode860 { + + @Test + public void test() { + System.out.println(new Solution().lemonadeChange(new int[]{5,5,10,10,20})); + } + + class Solution { + public boolean lemonadeChange(int[] bills) { + + int haveFive = 0; + int haveTen = 0; + for (int bill : bills) { + if (bill == 5) { + haveFive++; + } else if (bill == 10) { + haveTen++; + haveFive--; + } else if (bill == 20) { + if (haveTen >= 1) { + haveTen--; + haveFive--; + }else { + haveFive -= 3; + } + } + if (haveFive < 0 || haveTen < 0) { + return false; + } + } + return true; + + } + } +}