diff --git a/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode1005.java b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode1005.java new file mode 100644 index 0000000..ad4bbba --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode1005.java @@ -0,0 +1,113 @@ +package cn.whaifree.leetCode.Greedy; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.stream.IntStream; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/27 11:42 + * @注释 + */ +public class LeetCode1005 { + + @Test + public void test() { + System.out.println(new Solution().largestSumAfterKNegations(new int[]{-2,5,0,2,-2}, 3)); + System.out.println(new Solution().largestSumAfterKNegations(new int[]{4,2,3}, 1)) + ; + + System.out.println(new Solution1().largestSumAfterKNegations(new int[]{-2,5,0,2,-2}, 3)); + System.out.println(new Solution1().largestSumAfterKNegations(new int[]{4,2,3}, 1)); + } + + + class Solution { + + /** + * 注意几个例子 + * 1. nums = [3,-1,0,2], k = 3 选择下标 (1, 2, 2) ,nums 变为 [3,1,0,2] + * + * -2,5,1,2,-2 + * 按绝对值逆转 5 -2 2 -2 1 + * 每次遇到负数,变为相反数 + * // 剩下的k如果是奇数,就把最后一个逆转 + * + * @param nums + * @param k + * @return + */ + public int largestSumAfterKNegations(int[] nums, int k) { + // 按绝对值从大到小排序,对前k个负数进行相反数 + nums = IntStream.of(nums) + .boxed() + .sorted((o1, o2) -> Math.abs(o2) - Math.abs(o1)) + .mapToInt(Integer::intValue).toArray(); + + + for (int i = 0; i < nums.length; i++) { + if (k > 0 && nums[i] < 0) { + nums[i] = -nums[i]; + k--; + } + } + + // 此时还有k个没减去,k为偶数则不管,k为奇数就把最小那个变为正数 + if (k % 2 == 1) { + nums[nums.length-1] = -nums[nums.length-1]; + } + + + return Arrays.stream(nums).sum(); + } + } + + class Solution1 { + + /** + * 注意几个例子 + * 1. nums = [3,-1,0,2], k = 3 选择下标 (1, 2, 2) ,nums 变为 [3,1,0,2] + * + * -2,5,1,2,-2 + * 排序 + * -2 -2 1 2 5 + * 逆转 + * 2 2 1 2 5 + * 将最下的数逆转,sum-min-min + * 2 2 -1 2 5 + * + * @param nums + * @param k + * @return + */ + public int largestSumAfterKNegations(int[] nums, int k) { + // 按绝对值从大到小排序,对前k个负数进行相反数 + Arrays.sort(nums); + + + int sum = 0; + int minValue = Integer.MAX_VALUE; + for (int i = 0; i < nums.length; i++) { + if (k > 0 && nums[i] < 0) { + nums[i] = -nums[i]; + k--; + } + sum += nums[i]; + minValue = Math.min(minValue, nums[i]); + } + + // 此时还有k个没减去,k为偶数则不管,k为奇数就把最小那个变为正数 + if (k % 2 == 1) { + // 减去在遍历过程中加的minValue部分和通过变换负数的部分 + return sum - minValue - minValue; + }else { + return sum; + } + + } + } + +} diff --git a/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode134.java b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode134.java new file mode 100644 index 0000000..09397bb --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode134.java @@ -0,0 +1,105 @@ +package cn.whaifree.leetCode.Greedy; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/27 20:02 + * @注释 + */ +public class LeetCode134 { + + @Test + public void test() { + System.out.println(new Solution().canCompleteCircuit( + new int[]{5,1,2,3,4}, + new int[]{4,4,1,5,1} + )); + + } + + class Solution { + /** + * + * @param gas 加油站有的油 + * @param cost 行驶代价 + * @return + */ + public int canCompleteCircuit(int[] gas, int[] cost) { + + int[] rent = new int[gas.length]; + int total = 0; + for (int i = 0; i < gas.length; i++) { + rent[i] = gas[i] - cost[i]; + total += rent[i]; + } + // 如果全部剩余<0 则必然跑不了一圈 + if (total < 0) { + return -1; + } + + + // 以下为必然可以跑一圈的 + // 如果当前剩余不够用,则让指针指向i+1 + int curSum = 0; + int index = 0; + for (int i = 0; i < rent.length; i++) { + curSum += rent[i]; + if (curSum < 0) { + index = (i + 1) % gas.length ; + curSum = 0; + } + } + // 1. 前提,必然能跑一圈 + // 2. 没有进入某个i之后都没有curSum<0 那么就是正确的i + return index; + } + + } +// class Solution { +// /** +// * +// * @param gas 加油站有的油 +// * @param cost 行驶代价 +// * @return +// */ +// public int canCompleteCircuit(int[] gas, int[] cost) { +// int needSum = 0; +// for (int i : cost) { +// needSum += i; +// } +// +// int iHave = 0; +// int sumAdd = 0; +// int start = 0; +// for (int i = 0; i < gas.length; i++) { +// iHave += gas[i]; +// sumAdd += gas[i]; +// if (iHave < cost[i]) { +// iHave = 0; +// i = start; +// start = start + 1; +// sumAdd = 0; +// continue; +// } +// iHave -= cost[i]; +// if (sumAdd >= needSum) { +// return start; +// } +// +// if (i == gas.length - 1) { +// i = -1; +// } +// +// if (start == gas.length - 1 && start != i) { +// break; +// } +// } +// +// return -1; +// } +// +// } + +} diff --git a/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode135.java b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode135.java new file mode 100644 index 0000000..32fa45e --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode135.java @@ -0,0 +1,63 @@ +package cn.whaifree.leetCode.Greedy; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.Vector; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/28 12:44 + * @注释 + */ +public class LeetCode135 { + + @Test + public void tesr() { + System.out.println(new Solution().candy(new int[]{1, 2, 2, 5, 4, 3, 2})); + } + + + class Solution { + public int candy(int[] ratings) { + // 从前往后遍历,遇到ratings[i]>ratings[i-1] score[i]+1 + int length = ratings.length; + + int[] scores = new int[length]; + scores[0] = 1; + + for (int i = 1; i < length; i++) { + if (ratings[i] > ratings[i - 1]) { + scores[i] = scores[i - 1] + 1; + }else { + scores[i] = 1; + } + } + + // 从后往前 + for (int i = length -1; i > 0; i--) { + /** + * 如果 ratings[i] < ratings[i - 1],此时candyVec[i-1](第i-1个小孩的糖果数量)就有两个选择了, + * - 一个是candyVec[i] + 1(从右边这个加1得到的糖果数量), + * - 一个是candyVec[i-1](之前比较右孩子大于左孩子得到的糖果数量)。 + * 如 5 3 + * 从右边向左边,i-1可选择为 3+1 或者原来的 5,从左往右已经处理过的, + * 同时要满足同时保证i小孩既大于左也大于右边,那么取最大值。 + * 同时保证i小孩既大于左也大于右边 + */ + if (ratings[i] < ratings[i - 1]) { + scores[i - 1] = Math.max(scores[i] + 1, scores[i - 1]); + } + } + + int scoreSum = 0; + for (int score : scores) { + scoreSum += score; + } + + return scoreSum; + } + } + +} diff --git a/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode45.java b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode45.java new file mode 100644 index 0000000..04b653d --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode45.java @@ -0,0 +1,102 @@ +package cn.whaifree.leetCode.Greedy; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/27 11:01 + * @注释 + */ +public class LeetCode45 { + + @Test + public void test() { + System.out.println(new Solution().jump(new int[]{2,3,1,1,4})); + } + + class Solution { + /** + * 每个区间内只有一个最跳数 + * 每个区间只增加一次jump + * 1. 标记临时最大区间 + * 2. 标记区间结束时jump++ + * @param nums + * @return + */ + public int jump(int[] nums) { + + // 最远覆盖范围能否到达nums.length-1 + + if (nums.length == 1) { + return 0; + } + + // 最大覆盖范围 + int maxCover = 0; + + // 当前index覆盖范围区间 + int curInterval = 0; + int jumpCount = 0; + for (int i = 0; i < nums.length; i++) { + // 当前最大覆盖区间 + maxCover = Math.max(maxCover, i + nums[i]); + + // 必须等待区间增加后再判断是否到末位 + if (maxCover >= nums.length - 1) { + jumpCount++; + break; + } + + // 如果到了当前index覆盖的区间最后一个 + if (i == curInterval) { + jumpCount++; + curInterval = maxCover; + + } + + + } + return jumpCount; + } + } + + class Solution1 { + /** + * 每个区间内只有一个最跳数 + * 每个区间只增加一次jump + * 1. 标记临时最大区间 + * 2. 到达最大跳数++ + * @param nums + * @return + */ + public int jump(int[] nums) { + + // 最远覆盖范围能否到达nums.length-1 + + if (nums.length == 1) { + return 0; + } + + // 最大覆盖范围 + int maxCover = 0; + + // 当前index覆盖范围区间 + int curInterval = 0; + int jumpCount = 0; + for (int i = 0; i < nums.length - 1; i++) { + // if (nextDistance >= nums.size() - 1) break; + // 让nums.length变成num.length-1 本质是一样的 + + // 当前最大覆盖区间 + maxCover = Math.max(maxCover, i + nums[i]); + if (i == curInterval) { + jumpCount++; + curInterval = maxCover; + } + } + return jumpCount; + } + } + +} diff --git a/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode55.java b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode55.java new file mode 100644 index 0000000..ce8b71f --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode55.java @@ -0,0 +1,75 @@ +package cn.whaifree.leetCode.Greedy; + +import cn.whaifree.leetCode.Tree.LeetCode94; +import org.junit.Test; +import sun.misc.Launcher; + +import java.net.URL; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/26 11:33 + * @注释 + */ +public class LeetCode55 { + + @Test + public void test() { + System.out.println(new Solution().canJump(new int[]{2,0,0})); + + + + } + + + + + + class CustomClassLoader extends ClassLoader { + public CustomClassLoader(ClassLoader parent) { + super(parent); + } + + @Override + protected Class> findClass(String name) throws ClassNotFoundException { + byte[] res = getClassFromCustomClass(name); + if (res != null) { + return defineClass(name, res, 0, res.length); + } else { + throw new ClassNotFoundException(name); + } + } + + private byte[] getClassFromCustomClass(String name) { + //自定义路径加载指定类 + // 加密解密 + return null; + } + } + + class Solution { + /** + * 每个index都有一个cover覆盖范围,一旦这个范围可以覆盖nums,就能够返回 + * @param nums + * @return + */ + public boolean canJump(int[] nums) { + if (nums.length == 1) { + return true; + } + + // cover表示最远能到哪里 + int cover = 0; + // 注意这里cover表示能够覆盖的最远范围,不能超过他 + for (int i = 0; i <= cover; i++) { + cover = Math.max(i + nums[i], cover); + if (cover >= nums.length - 1) { + return true; + } + } + return false; + } + + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode860.java b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode860.java new file mode 100644 index 0000000..9d4c63b --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Greedy/LeetCode860.java @@ -0,0 +1,58 @@ +package cn.whaifree.leetCode.Greedy; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/2/28 13:22 + * @注释 + */ +public class LeetCode860 { + + @Test + public void test() { + System.out.println(new Solution().lemonadeChange(new int[]{5,5,10,10,20})); + } + + class Solution { + + /** + * bills[i] 不是 5 就是 10 或是 20 + * @param bills + * @return + */ + public boolean lemonadeChange(int[] bills) { + + int fiveHave = 0; // 有5元的钞票数 + int tenHave = 0; // 10元钞票数 + for (int i = 0; i < bills.length; i++) { + + if (bills[i] == 5) { // 5元直接收下 + fiveHave++; + } else if (bills[i] == 10) { // 10元则找5元 + tenHave++; + fiveHave--; + } else if (bills[i] == 20) { // 20元可以选择给他10+5或者3个5元 + if (tenHave >= 1) { + tenHave--; + fiveHave--; + } else { + fiveHave -= 3; + } + } + // 如果钞票不够找 + if (fiveHave < 0 || tenHave < 0) { + return false; + } + } + return true; + } + + + } +} diff --git a/src/main/java/cn/whaifree/leetCode/T.java b/src/main/java/cn/whaifree/leetCode/T.java new file mode 100644 index 0000000..f819f69 --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/T.java @@ -0,0 +1,102 @@ +package cn.whaifree.leetCode; + +/** + * @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"); + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Test.java b/src/main/java/cn/whaifree/leetCode/Test.java index 31c35f0..74fe730 100644 --- a/src/main/java/cn/whaifree/leetCode/Test.java +++ b/src/main/java/cn/whaifree/leetCode/Test.java @@ -11,66 +11,130 @@ import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** - * * 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。 - * + *
* 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。 - * + *
* 你可以按任意顺序返回答案。 - * + *
* 来源:力扣(LeetCode) * 链接:https://leetcode.cn/problems/two-sum * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 - * - * - * + *
+ *
+ *
* 输入:nums = [2,7,11,15], target = 9
* 输出:[0,1]
* 解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
*
- *
* @author whaifree
* @package cn.whaifree.leetCode
* @Date: 2022/9/13 20:54
*/
-public class Test {
+public class Test {
+
+ private static int number;
+
+ private int getNumber;
- static {
- num = 20;
- // 防止在初始化前程序中访问默认值
+ public int minus() {
+ localVarl();
+ int i = 1;
+ double d = 2d;
+ char c = 'd';
+ return (int) (i + d);
}
- private static int num = 10;
+
+ public void localVarl() {
+ int a = 0;
+ System.out.println(a);
+ int b = 0;
+ }
+
+ public void localVar2() {
+ {
+ int a = 0;
+ System.out.println(a);
+ }
+ //此时的就会复用a的槽位
+ int b = 0;
+ }
+
public static void main(String[] args) {
- ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
- System.out.println(systemClassLoader);
- ClassLoader extClassLoader = systemClassLoader.getParent();
- System.out.println(extClassLoader);
-
- // 获取不到引导类类加载器
- ClassLoader bootClassLoader = extClassLoader.getParent();
- System.out.println(bootClassLoader);
-
- ClassLoader classLoader = Test.class.getClassLoader();
- System.out.println(classLoader);
+ //第1类问题
+ int i1 = 10;
+ i1++;
+ int i2 = 10;
+ ++i2;
- // 系统核心类库都是使用引导类(BootStrapClassLoader)进行加载,都为null
- ClassLoader stringLoader = String.class.getClassLoader();
- System.out.println(stringLoader);
+ //第2类问题:
+ int i3 = 10;
+ int i4 = i3++;
+ int i5 = 10;
+ int i6 = ++i5;
- PriorityQueue