leecode更新
This commit is contained in:
parent
4e78e2be81
commit
79f88b6081
|
|
@ -0,0 +1,139 @@
|
||||||
|
package com.markilue.leecode.array;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import javax.management.remote.rmi._RMIConnection_Stub;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 寻找长度最小的子数组
|
||||||
|
*/
|
||||||
|
public class MinSubArrayLen {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
|
||||||
|
int target = 3;
|
||||||
|
int[] nums = {1, 1, 1};
|
||||||
|
System.out.println(minSubArrayLen2(target, nums));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 代码随想录中解法:可伸缩的滑动窗口
|
||||||
|
* 看了思路之后自己尝试
|
||||||
|
* 速度击败20% 内存击败60%
|
||||||
|
* @param target
|
||||||
|
* @param nums
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public int minSubArrayLen(int target, int[] nums) {
|
||||||
|
|
||||||
|
int start = 0;
|
||||||
|
int stop = 0;
|
||||||
|
//记录窗口总和
|
||||||
|
int sum = nums[start];
|
||||||
|
//记录窗口长度
|
||||||
|
int len = 0;
|
||||||
|
//记录结果
|
||||||
|
int result = nums.length;
|
||||||
|
boolean flag = false;
|
||||||
|
while (stop <= nums.length) {
|
||||||
|
|
||||||
|
while (sum >= target) {
|
||||||
|
flag = true;
|
||||||
|
len = stop - start + 1;
|
||||||
|
if (result > len) {
|
||||||
|
result = len;
|
||||||
|
}
|
||||||
|
if (len == 1) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
sum -= nums[start++];
|
||||||
|
}
|
||||||
|
if (len == 1 && sum > target) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (stop == nums.length - 1) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
sum += nums[++stop];
|
||||||
|
}
|
||||||
|
if (flag) {
|
||||||
|
return result;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 代码随想录中解法:可伸缩的滑动窗口
|
||||||
|
* 官方实现:速度击败99.99%,内存击败58.84%
|
||||||
|
*
|
||||||
|
* @param target
|
||||||
|
* @param nums
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public int minSubArrayLen1(int target, int[] nums) {
|
||||||
|
|
||||||
|
int start = 0;
|
||||||
|
int sum = 0;
|
||||||
|
int result = Integer.MAX_VALUE;
|
||||||
|
int len = 0;
|
||||||
|
|
||||||
|
for (int stop = 0; stop < nums.length; stop++) {
|
||||||
|
sum += nums[stop];
|
||||||
|
while (sum >= target) {
|
||||||
|
len = stop - start + 1;
|
||||||
|
result = result > len ? len : result;
|
||||||
|
sum -= nums[start++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result == Integer.MAX_VALUE ? 0 : result;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 评论里给的滑动窗口法优化:
|
||||||
|
* 目前没看出优化在哪里,感觉没必要
|
||||||
|
* @param s
|
||||||
|
* @param nums
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public int minSubArrayLen2(int s, int[] nums) {
|
||||||
|
if (nums.length == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int count = 0;
|
||||||
|
int left = 0;
|
||||||
|
int right = 0;
|
||||||
|
while (count < s) {
|
||||||
|
if (right == nums.length) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
count += nums[right];
|
||||||
|
right++;
|
||||||
|
}
|
||||||
|
while (right < nums.length) {
|
||||||
|
if (count < s) {
|
||||||
|
count += nums[right];
|
||||||
|
right++;
|
||||||
|
}
|
||||||
|
count -= nums[left];
|
||||||
|
left++;
|
||||||
|
}
|
||||||
|
while (count >= s) {
|
||||||
|
count -= nums[left];
|
||||||
|
left++;
|
||||||
|
}
|
||||||
|
return right - left + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,62 @@
|
||||||
|
package com.markilue.leecode.array;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 螺旋矩阵:
|
||||||
|
* 给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix
|
||||||
|
*/
|
||||||
|
public class generateMatrix {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test(){
|
||||||
|
|
||||||
|
int[][] ints = generateMatrix(6);
|
||||||
|
for (int[] anInt : ints) {
|
||||||
|
System.out.println(Arrays.toString(anInt));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 看了代码随想录之后的自己编程:寻找循环不变量
|
||||||
|
* 速度击败100%,内存击败87%
|
||||||
|
* 不会就先把第一层循环写出来,在找规律
|
||||||
|
* @param n
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public int[][] generateMatrix(int n) {
|
||||||
|
int[][] result = new int[n][n];
|
||||||
|
int count = 1;
|
||||||
|
int i=0;
|
||||||
|
int j=0;
|
||||||
|
for (int k = 0; k < n / 2; k++) {
|
||||||
|
|
||||||
|
for (; i < result[k].length-k-1; i++) {
|
||||||
|
result[j][i]=count++;
|
||||||
|
}
|
||||||
|
for (; j < result[k].length-k-1; j++) {
|
||||||
|
result[j][i]=count++;
|
||||||
|
}
|
||||||
|
for (;i>k;i--){
|
||||||
|
result[j][i]=count++;
|
||||||
|
}
|
||||||
|
for (;j>k;j--){
|
||||||
|
result[j][i]=count++;
|
||||||
|
}
|
||||||
|
i+=1;
|
||||||
|
j+=1;
|
||||||
|
|
||||||
|
}
|
||||||
|
//如果是奇数,单独填充中间的
|
||||||
|
if(n%2==1){
|
||||||
|
result[n/2][n/2]=count++;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue