Merge branch 'master' of https://gitee.com/huangge1199_admin/leet-code
Conflicts: src/main/java/leetcode/editor/cn/all.json
This commit is contained in:
commit
df77d05305
@ -0,0 +1,24 @@
|
||||
//package com.code.leet.doubleWeek;
|
||||
//
|
||||
//public class MovieRentingSystem {
|
||||
//
|
||||
// public MovieRentingSystem(int n, int[][] entries) {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// public List<Integer> search(int movie) {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// public void rent(int shop, int movie) {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// public void drop(int shop, int movie) {
|
||||
//
|
||||
// }
|
||||
//
|
||||
// public List<List<Integer>> report() {
|
||||
//
|
||||
// }
|
||||
//}
|
72
src/main/java/com/code/leet/doubleWeek/SolutionD55.java
Normal file
72
src/main/java/com/code/leet/doubleWeek/SolutionD55.java
Normal file
@ -0,0 +1,72 @@
|
||||
package com.code.leet.doubleWeek;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class SolutionD55 {
|
||||
public static void main(String[] args) {
|
||||
SolutionD55 solution = new SolutionD55();
|
||||
// System.out.println(solution.canBeIncreasing(new int[]{2, 3, 1, 2}));
|
||||
System.out.println(solution.maxAlternatingSum(new int[]{2, 1, 5, 4, 4}));
|
||||
}
|
||||
|
||||
public boolean canBeIncreasing(int[] nums) {
|
||||
return countDel(nums, 0) < 2;
|
||||
}
|
||||
|
||||
private int countDel(int[] nums, int count) {
|
||||
int size = nums.length - 1;
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (nums[i] >= nums[i + 1]) {
|
||||
count++;
|
||||
int[] aft;
|
||||
int[] bef;
|
||||
if (i > 0) {
|
||||
bef = new int[size - i + 1];
|
||||
bef[0] = nums[i - 1];
|
||||
for (int j = i + 1; j <= size; j++) {
|
||||
bef[j - i] = nums[j];
|
||||
}
|
||||
aft = Arrays.copyOf(bef, size - i + 1);
|
||||
aft[1] = nums[i];
|
||||
} else {
|
||||
bef = Arrays.copyOfRange(nums, 1, size);
|
||||
aft = bef;
|
||||
}
|
||||
count = Math.min(countDel(bef, count), countDel(aft, count));
|
||||
if (count > 2) {
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public String removeOccurrences(String s, String part) {
|
||||
while (s.contains(part)) {
|
||||
s = s.replace(part, "");
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public long maxAlternatingSum(int[] nums) {
|
||||
if (nums.length == 1) {
|
||||
return nums[0];
|
||||
}
|
||||
if (nums.length == 2) {
|
||||
return Math.max(nums[0], nums[1]);
|
||||
}
|
||||
int[] temp = Arrays.copyOfRange(nums, 1, nums.length);
|
||||
return Math.max(nums[0] - min(temp), maxAlternatingSum(temp));
|
||||
}
|
||||
|
||||
private long min(int[] nums) {
|
||||
if (nums.length == 1) {
|
||||
return nums[0];
|
||||
}
|
||||
if (nums.length == 2) {
|
||||
return Math.min(nums[0] - nums[1], nums[1] - nums[0]);
|
||||
}
|
||||
int[] temp = Arrays.copyOfRange(nums, 1, nums.length);
|
||||
return Math.min(nums[0] - maxAlternatingSum(temp), min(temp));
|
||||
}
|
||||
}
|
39
src/main/java/com/code/leet/entiy/LCArray.java
Normal file
39
src/main/java/com/code/leet/entiy/LCArray.java
Normal file
@ -0,0 +1,39 @@
|
||||
package com.code.leet.entiy;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class LCArray {
|
||||
|
||||
int[] arr;
|
||||
int curIndex;
|
||||
int maxSize;
|
||||
|
||||
public LCArray() {
|
||||
arr = new int[1];
|
||||
curIndex = 0;
|
||||
maxSize = 1;
|
||||
}
|
||||
|
||||
public void push_back(int n) {
|
||||
if(curIndex==maxSize){
|
||||
maxSize *= 2;
|
||||
int[] temp = new int[maxSize];
|
||||
if (curIndex >= 0) System.arraycopy(arr, 0, temp, 0, curIndex);
|
||||
arr = temp;
|
||||
}
|
||||
arr[curIndex] = n;
|
||||
curIndex++;
|
||||
}
|
||||
|
||||
public void pop_back() {
|
||||
curIndex--;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return curIndex;
|
||||
}
|
||||
|
||||
public int index(int idx) {
|
||||
return arr[idx];
|
||||
}
|
||||
}
|
95
src/main/java/com/code/leet/week/Solution247.java
Normal file
95
src/main/java/com/code/leet/week/Solution247.java
Normal file
@ -0,0 +1,95 @@
|
||||
package com.code.leet.week;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Solution247 {
|
||||
public static void main(String[] args) {
|
||||
Solution247 solution = new Solution247();
|
||||
solution.rotateGrid(new int[][]{{40,10},{30,20}},1);
|
||||
}
|
||||
|
||||
public int maxProductDifference(int[] nums) {
|
||||
Arrays.sort(nums);
|
||||
int size = nums.length;
|
||||
return nums[size - 1] * nums[size - 2] - nums[0] * nums[1];
|
||||
}
|
||||
|
||||
|
||||
public int[][] rotateGrid(int[][] grid, int k) {
|
||||
int xSize = grid.length;
|
||||
int ySize = grid[0].length;
|
||||
int round = Math.min(xSize, ySize) / 2;
|
||||
for (int i = 0; i < round; i++) {
|
||||
int sum = (xSize - 1 - i) * 2 + (ySize - 1 - i) * 2;
|
||||
int temp = k % sum;
|
||||
if (temp == 0) {
|
||||
continue;
|
||||
}
|
||||
List<Integer> list = new ArrayList<>();
|
||||
for (int y = i; y < ySize - i - 1; y++) {
|
||||
list.add(grid[i][y]);
|
||||
}
|
||||
for (int x = i; x < xSize - i - 1; x++) {
|
||||
list.add(grid[x][ySize - i - 1]);
|
||||
}
|
||||
for (int y = ySize - i - 1; y > i; y--) {
|
||||
list.add(grid[xSize - i - 1][y]);
|
||||
}
|
||||
for (int x = xSize - i - 1; x > i; x--) {
|
||||
list.add(grid[x][i]);
|
||||
}
|
||||
List<Integer> trans = new ArrayList<>();
|
||||
for (int j = 0; j < list.size(); j++) {
|
||||
if (j + k < list.size()) {
|
||||
trans.add(list.get(j + k));
|
||||
} else {
|
||||
trans.add(list.get(j + k - trans.size()));
|
||||
}
|
||||
}
|
||||
int index=0;
|
||||
for (int y = i; y < ySize - i - 1; y++) {
|
||||
grid[i][y] = trans.get(index);
|
||||
index++;
|
||||
}
|
||||
for (int x = i; x < xSize - i - 1; x++) {
|
||||
grid[x][ySize - i - 1] = trans.get(index);
|
||||
index++;
|
||||
}
|
||||
for (int y = ySize - i - 1; y > i; y--) {
|
||||
grid[xSize - i - 1][y] = trans.get(index);
|
||||
index++;
|
||||
}
|
||||
for (int x = xSize - i - 1; x > i; x--) {
|
||||
grid[x][i] = trans.get(index);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
||||
// public long wonderfulSubstrings(String word) {
|
||||
// char[] chs = word.toCharArray();
|
||||
// int count = chs.length;
|
||||
// for (int i = 0; i < chs.length; i++) {
|
||||
// Map<Character, Integer> map = new HashMap<>();
|
||||
// map.put(chs[i], 1);
|
||||
// int odd = 1;
|
||||
// int enev = 0;
|
||||
// for (int j = i + 1; j < chs.length; j++) {
|
||||
// int num = map.getOrDefault(chs[j], 0) + 1;
|
||||
// if (num % 2 == 1) {
|
||||
// odd++;
|
||||
// enev--;
|
||||
// } else {
|
||||
// odd--;
|
||||
// enev++;
|
||||
// }
|
||||
// if (odd < 2) {
|
||||
// count++;
|
||||
// }
|
||||
// map.put(chs[j],num);
|
||||
// }
|
||||
// }
|
||||
// return count;
|
||||
// }
|
||||
}
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user