hot100数组

This commit is contained in:
linlihong 2024-08-19 16:08:54 +08:00
commit ea7b104794
9 changed files with 295 additions and 0 deletions

39
.gitignore vendored Normal file
View File

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

26
pom.xml Normal file
View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cool</groupId>
<artifactId>LeetCode</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,7 @@
package com.cool;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}

View File

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

View File

@ -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<end){
int temp=nums[end];
nums[end]=nums[start];
nums[start]=temp;
start++;
end--;
}
}
}

View File

@ -0,0 +1,29 @@
package com.cool.hot100.common_arr;
public class Num238 {
/**
* Date 2024/8/19
* DayNumber 3
*
* @param nums
* @return
*/
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] lArr = new int[n];
int[] rArr = new int[n];
lArr[0] = 1;
rArr[n - 1] = 1;
for (int i = 1; i < n; i++) {
lArr[i] = lArr[i - 1] * nums[i - 1];
// rArr[n - i - 1] = rArr[n - i] * nums[n - i];
}
int R = 1;
for (int i = n - 2; i >= 0; i--) {
R *= nums[i + 1];
lArr[i] *= R;
}
return lArr;
}
}

View File

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

View File

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

View File

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