commit ea7b104794f77b4e23d6c6e4f185893d38a03eb1 Author: linlihong <747682928@qq.com> Date: Mon Aug 19 16:08:54 2024 +0800 hot100数组 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..99ae635 --- /dev/null +++ b/.gitignore @@ -0,0 +1,39 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +/.idea +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..70e1ab9 --- /dev/null +++ b/pom.xml @@ -0,0 +1,26 @@ + + + 4.0.0 + + com.cool + LeetCode + 1.0-SNAPSHOT + + + 8 + 8 + UTF-8 + + + + + + junit + junit + 4.13.2 + + + + diff --git a/src/main/java/com/cool/Main.java b/src/main/java/com/cool/Main.java new file mode 100644 index 0000000..c86040f --- /dev/null +++ b/src/main/java/com/cool/Main.java @@ -0,0 +1,7 @@ +package com.cool; + +public class Main { + public static void main(String[] args) { + System.out.println("Hello world!"); + } +} \ No newline at end of file diff --git a/src/main/java/com/cool/Template.java b/src/main/java/com/cool/Template.java new file mode 100644 index 0000000..3f254d3 --- /dev/null +++ b/src/main/java/com/cool/Template.java @@ -0,0 +1,14 @@ +package com.cool; + +public class Template { + + /** + * Date 2924/8/19 + * DayNumber 1/2/3/4 + * @param template + */ + public void test(int template){ + + } + +} diff --git a/src/main/java/com/cool/hot100/common_arr/Num189.java b/src/main/java/com/cool/hot100/common_arr/Num189.java new file mode 100644 index 0000000..3d52e67 --- /dev/null +++ b/src/main/java/com/cool/hot100/common_arr/Num189.java @@ -0,0 +1,32 @@ +package com.cool.hot100.common_arr; + +public class Num189 { + + + /** + * Date 2024/8/19 + * DayNumber 2 + */ + public void rotate(int[] nums, int k) { +// +// int[] newArr = new int[nums.length]; +// for (int i = 0; i < newArr.length; i++) { +// newArr[(i + k)%nums.length] = nums[i]; +// } +// System.arraycopy(newArr,0,nums,0,nums.length); + reverse(nums,0,nums.length-1); + reverse(nums,0,k%nums.length-1); + reverse(nums,k%nums.length,nums.length-1); + + } + + public void reverse(int[]nums,int start,int end){ + while(start= 0; i--) { + R *= nums[i + 1]; + lArr[i] *= R; + } + return lArr; + } +} diff --git a/src/main/java/com/cool/hot100/common_arr/Num41.java b/src/main/java/com/cool/hot100/common_arr/Num41.java new file mode 100644 index 0000000..65cb94d --- /dev/null +++ b/src/main/java/com/cool/hot100/common_arr/Num41.java @@ -0,0 +1,51 @@ +package com.cool.hot100.common_arr; + +import org.junit.Test; + +import java.util.Arrays; + +public class Num41 { + + + /** + * DayNumber 4 + * Date 2024/8/19 + */ + public int firstMissingPositive(int[] nums) { +// Arrays.sort(nums); +// int R=1; +// for (int num : nums) { +// if(num>0){ +// if(num>R){ +// return R; +// }else if(num==R) { +// R++; +// } +// } +// } +// return R; + int n = nums.length; + for (int i = 0; i < n; ++i) { + if (nums[i] <= 0) { + nums[i] = n + 1; + } + } + for (int i = 0; i < n; i++) { + int num = Math.abs(nums[i]); + if (num <= n) { + nums[num - 1] = -Math.abs(nums[num - 1]); + } + } + for (int i = 0; i < n; i++) { + if (nums[i] > 0) { + return i + 1; + } + } + return n + 1; + } + + @Test + public void test() { + System.out.println(firstMissingPositive(new int[]{1, 1})); + } +} diff --git a/src/main/java/com/cool/hot100/common_arr/Num53.java b/src/main/java/com/cool/hot100/common_arr/Num53.java new file mode 100644 index 0000000..eb68323 --- /dev/null +++ b/src/main/java/com/cool/hot100/common_arr/Num53.java @@ -0,0 +1,51 @@ +package com.cool.hot100.common_arr; + +import org.junit.Test; + +public class Num53 { + + /** + * 53. 最大子序和 + */ + /** + * 给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 + * 子数组 + * 是数组中的一个连续部分。 + * 示例 1: + * 输入:nums = [-2,1,-3,4,-1,2,1,-5,4] + * 输出:6 + * 解释:连续子数组 [4,-1,2,1] 的和最大,为 6 。 + * 示例 2: + * 输入:nums = [1] + * 输出:1 + * 示例 3: + * 输入:nums = [5,4,-1,7,8] + * 输出:23 + * + * @param nums + * @return + */ + + public int maxSubArray(int[] nums) { + int left = 0; + int right = 0; + int res = Integer.MIN_VALUE; + int sum = 0; + while (right < nums.length) { + sum += nums[right++]; + if (sum >= 0) { + } else { + sum = 0; + } + res = Math.max(sum, res); + } + return res; + } + + @Test + public void test() { + System.out.println(maxSubArray(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4})); + } + + +} diff --git a/src/main/java/com/cool/hot100/common_arr/Num56.java b/src/main/java/com/cool/hot100/common_arr/Num56.java new file mode 100644 index 0000000..5c44734 --- /dev/null +++ b/src/main/java/com/cool/hot100/common_arr/Num56.java @@ -0,0 +1,46 @@ +package com.cool.hot100.common_arr; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Num56 { + + + /** + * Date 2024/8/19 + * DayNumber 1 + * 以数组 intervals 表示若干个区间的集合,其中单个区间为 intervals[i] = [starti, endi] 。请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。 + * 示例 1: + * 输入:intervals = [[1,3],[2,6],[8,10],[15,18]] + * 输出:[[1,6],[8,10],[15,18]] + * 解释:区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6]. + * 示例 2: + * 输入:intervals = [[1,4],[4,5]] + * 输出:[[1,5]] + * 解释:区间 [1,4] 和 [4,5] 可被视为重叠区间。 + */ + public int[][] merge(int[][] intervals) { + Arrays.sort(intervals, (p, q) -> p[0] - q[0]); // 按照左端点从小到大排序 + List list=new ArrayList<>(); + list.add(intervals[0]); + for (int[] interval : intervals) { + int index = list.size() - 1; + if (list.get(index)[1] < interval[0]) { + list.add(interval); + } else { + list.get(index)[1] = Math.max(list.get(index)[1], interval[1]); + } + + } + return list.toArray(new int[list.size()][]); + } + + + @Test + public void test(){ + merge(new int[][]{{2,3},{2,2},{3,3},{1,3},{5,7},{2,2},{4,6}}); + } +}