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
6e2eca177b
72
src/main/java/com/code/leet/doubleWeek/SolutionD53.java
Normal file
72
src/main/java/com/code/leet/doubleWeek/SolutionD53.java
Normal file
@ -0,0 +1,72 @@
|
||||
package com.code.leet.doubleWeek;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class SolutionD53 {
|
||||
public static void main(String[] args) {
|
||||
SolutionD53 solution = new SolutionD53();
|
||||
// System.out.println(Arrays.toString(solution.getBiggestThree(new int[][]{{3, 4, 5, 1, 3}, {3, 3, 4, 2, 3}, {20, 30, 200, 40, 10}, {1, 5, 5, 4, 1}, {4, 3, 2, 2, 5}})));
|
||||
System.out.println(Arrays.toString(solution.getBiggestThree(new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}})));
|
||||
System.out.println(Arrays.toString(solution.getBiggestThree(new int[][]{{7, 7, 7}})));
|
||||
}
|
||||
|
||||
public int countGoodSubstrings(String s) {
|
||||
if (s.length() < 3) {
|
||||
return 0;
|
||||
}
|
||||
int count = 0;
|
||||
for (int i = 1; i < s.length() - 1; i++) {
|
||||
if (s.charAt(i - 1) != s.charAt(i) && s.charAt(i - 1) != s.charAt(i + 1) && s.charAt(i + 1) != s.charAt(i)) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public int minPairSum(int[] nums) {
|
||||
Arrays.sort(nums);
|
||||
int max = Integer.MIN_VALUE;
|
||||
int length = nums.length;
|
||||
for (int i = 0; i < length / 2; i++) {
|
||||
max = Math.max(nums[i] + nums[length - 1 - i], max);
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
public int[] getBiggestThree(int[][] grid) {
|
||||
Set<Integer> set = new HashSet<>();
|
||||
new TreeSet<>(Comparator.reverseOrder());
|
||||
int xLength = grid.length;
|
||||
int yLength = grid[0].length;
|
||||
int size = (Math.min(xLength, yLength) + 1) / 2;
|
||||
for (int i = 0; i < size; i++) {
|
||||
for (int j = i; j < xLength - i; j++) {
|
||||
for (int k = i; k < yLength - i; k++) {
|
||||
int sum = 0;
|
||||
for (int x = j - i; x <= j + i; x++) {
|
||||
int cha = i - Math.abs(j - x);
|
||||
if (Math.abs(j - x) == i) {
|
||||
sum += grid[x][k + cha];
|
||||
} else {
|
||||
sum += grid[x][k - cha] + grid[x][k + cha];
|
||||
}
|
||||
}
|
||||
set.add(sum);
|
||||
}
|
||||
}
|
||||
}
|
||||
List<Integer> list = set.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
|
||||
int length = Math.min(list.size(), 3);
|
||||
int[] result = new int[length];
|
||||
for (int i = 0; i < length; i++) {
|
||||
result[i] = list.get(i);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public int minimumXORSum(int[] nums1, int[] nums2) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
142
src/main/java/com/code/leet/week/Solution243.java
Normal file
142
src/main/java/com/code/leet/week/Solution243.java
Normal file
@ -0,0 +1,142 @@
|
||||
package com.code.leet.week;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Solution243 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Solution243 solution = new Solution243();
|
||||
// //2,2,0,2,1,2
|
||||
// System.out.println(solution.assignTasks(new int[]{3, 3, 2}, new int[]{1, 2, 3, 2, 1, 2}));
|
||||
System.out.println(solution.minSkips(new int[]{7,3,5,5},2,10));
|
||||
}
|
||||
|
||||
public boolean isSumEqual(String firstWord, String secondWord, String targetWord) {
|
||||
return trans(firstWord) + trans(secondWord) == trans(targetWord);
|
||||
}
|
||||
|
||||
private int trans(String str) {
|
||||
while (str.length() > 1 && str.startsWith("a")) {
|
||||
str = str.substring(1);
|
||||
}
|
||||
StringBuilder numStr = new StringBuilder();
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
numStr.append(str.charAt(i) - 'a');
|
||||
}
|
||||
return Integer.parseInt(numStr.toString());
|
||||
}
|
||||
|
||||
public String maxValue(String n, int x) {
|
||||
int i;
|
||||
if (n.startsWith("-")) {
|
||||
i = 1;
|
||||
for (; i < n.length(); i++) {
|
||||
if (n.charAt(i) - '0' > x) {
|
||||
n = n.substring(0, i) + x + n.substring(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
i = 0;
|
||||
for (; i < n.length(); i++) {
|
||||
if (n.charAt(i) - '0' < x) {
|
||||
n = n.substring(0, i) + x + n.substring(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (i == n.length()) {
|
||||
n += x;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
public int[] assignTasks(int[] servers, int[] tasks) {
|
||||
int sLength = servers.length;
|
||||
int tLength = tasks.length;
|
||||
int[] result = new int[tLength];
|
||||
int[] temp = new int[sLength];
|
||||
int index = 0;
|
||||
int count = 0;
|
||||
List<Integer> list = new ArrayList<>();
|
||||
Map<Integer, List<Integer>> map = new HashMap<>();
|
||||
int min = Integer.MAX_VALUE;
|
||||
int max = Integer.MIN_VALUE;
|
||||
for (int i = 0; i < sLength; i++) {
|
||||
min = Math.min(min, servers[i]);
|
||||
max = Math.max(max, servers[i]);
|
||||
List<Integer> tList = map.getOrDefault(servers[i], new ArrayList<>());
|
||||
tList.add(i);
|
||||
map.put(servers[i], tList);
|
||||
}
|
||||
for (int i = min; i <= max; i++) {
|
||||
if (map.containsKey(i)) {
|
||||
list.addAll(map.get(i));
|
||||
}
|
||||
}
|
||||
while (index < tLength) {
|
||||
int i = 0;
|
||||
for (; i < sLength; i++) {
|
||||
if (temp[list.get(i)] == 0) {
|
||||
result[index] = list.get(i);
|
||||
temp[list.get(i)] = tasks[index];
|
||||
index++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (count == 0) {
|
||||
for (int j = 0; j < sLength; j++) {
|
||||
temp[j] = Math.max(0, temp[j] - 1);
|
||||
}
|
||||
} else {
|
||||
count--;
|
||||
}
|
||||
if (i == sLength) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public int minSkips(int[] dist, int speed, int hoursBefore) {
|
||||
int length = dist.length;
|
||||
double[] free = new double[length-1];
|
||||
int[] hours = new int[length-1];
|
||||
double sum = 0;
|
||||
double hour = 0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
double temp = (double) dist[i] / speed;
|
||||
if (i == length - 1) {
|
||||
hour += (double) dist[i] / speed;
|
||||
sum += (double) dist[i] / speed;
|
||||
} else {
|
||||
hours[i] = dist[i] % speed == 0 ? dist[i] / speed : dist[i] / speed + 1;
|
||||
hour += hours[i];
|
||||
free[i] = 1 + (int) temp - temp;
|
||||
int j = i;
|
||||
while (j>=0){
|
||||
if(free[j]+free[j-1]>=1)
|
||||
if(hours[j]>1){
|
||||
break;
|
||||
}
|
||||
}
|
||||
sum += temp;
|
||||
}
|
||||
}
|
||||
if (sum > hoursBefore) {
|
||||
return -1;
|
||||
}
|
||||
if(hour<=hoursBefore){
|
||||
return 0;
|
||||
}
|
||||
Arrays.sort(free);
|
||||
int count = 0;
|
||||
for (int i = length - 2; i>=0; i--) {
|
||||
count++;
|
||||
if(hour-free[i]<=hoursBefore){
|
||||
break;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
@ -0,0 +1,93 @@
|
||||
//给出矩阵 matrix 和目标值 target,返回元素总和等于目标值的非空子矩阵的数量。
|
||||
//
|
||||
// 子矩阵 x1, y1, x2, y2 是满足 x1 <= x <= x2 且 y1 <= y <= y2 的所有单元 matrix[x][y] 的集合。
|
||||
//
|
||||
//
|
||||
// 如果 (x1, y1, x2, y2) 和 (x1', y1', x2', y2') 两个子矩阵中部分坐标不同(如:x1 != x1'),那么这两个子矩阵
|
||||
//也不同。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
//输入:matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0
|
||||
//输出:4
|
||||
//解释:四个只含 0 的 1x1 子矩阵。
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:matrix = [[1,-1],[-1,1]], target = 0
|
||||
//输出:5
|
||||
//解释:两个 1x2 子矩阵,加上两个 2x1 子矩阵,再加上一个 2x2 子矩阵。
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:matrix = [[904]], target = 0
|
||||
//输出:0
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= matrix.length <= 100
|
||||
// 1 <= matrix[0].length <= 100
|
||||
// -1000 <= matrix[i] <= 1000
|
||||
// -10^8 <= target <= 10^8
|
||||
//
|
||||
// Related Topics 数组 动态规划 Sliding Window
|
||||
// 👍 117 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
//1074:元素和为目标值的子矩阵数量
|
||||
class NumberOfSubmatricesThatSumToTarget {
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new NumberOfSubmatricesThatSumToTarget().new Solution();
|
||||
System.out.println(solution.numSubmatrixSumTarget(new int[][]{{0,1,0}, {1,1,1},{0,1,0}}, 0));
|
||||
}
|
||||
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public int numSubmatrixSumTarget(int[][] matrix, int target) {
|
||||
int xLength = matrix.length;
|
||||
int yLength = matrix[0].length;
|
||||
int[][] sum = new int[xLength + 1][yLength + 1];
|
||||
for (int i = 1; i <= xLength; ++i) {
|
||||
for (int j = 1; j <= yLength; ++j) {
|
||||
sum[i][j] = matrix[i - 1][j - 1] + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
|
||||
}
|
||||
}
|
||||
int count = 0;
|
||||
for (int i = 1; i <= xLength; ++i) {
|
||||
for (int j = 1; j <= yLength; ++j) {
|
||||
for (int k = 1; k <= i; ++k) {
|
||||
for (int l = 1; l <= j; ++l) {
|
||||
if (sum[i][j] - sum[k - 1][j] - sum[i][l - 1] + sum[k - 1][l - 1] == target){
|
||||
++count;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
<p>给出矩阵 <code>matrix</code> 和目标值 <code>target</code>,返回元素总和等于目标值的非空子矩阵的数量。</p>
|
||||
|
||||
<p>子矩阵 <code>x1, y1, x2, y2</code> 是满足 <code>x1 <= x <= x2</code> 且 <code>y1 <= y <= y2</code> 的所有单元 <code>matrix[x][y]</code> 的集合。</p>
|
||||
|
||||
<p>如果 <code>(x1, y1, x2, y2)</code> 和 <code>(x1', y1', x2', y2')</code> 两个子矩阵中部分坐标不同(如:<code>x1 != x1'</code>),那么这两个子矩阵也不同。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/09/02/mate1.jpg" style="width: 242px; height: 242px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>四个只含 0 的 1x1 子矩阵。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>matrix = [[1,-1],[-1,1]], target = 0
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>两个 1x2 子矩阵,加上两个 2x1 子矩阵,再加上一个 2x2 子矩阵。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>matrix = [[904]], target = 0
|
||||
<strong>输出:</strong>0
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong><strong>提示:</strong></strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= matrix.length <= 100</code></li>
|
||||
<li><code>1 <= matrix[0].length <= 100</code></li>
|
||||
<li><code>-1000 <= matrix[i] <= 1000</code></li>
|
||||
<li><code>-10^8 <= target <= 10^8</code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>数组</li><li>动态规划</li><li>Sliding Window</li></div></div>\n<div><li>👍 116</li><li>👎 0</li></div>
|
@ -0,0 +1,34 @@
|
||||
class Solution {
|
||||
public int numSubmatrixSumTarget(int[][] matrix, int target) {
|
||||
int xLength = matrix.length;
|
||||
int yLength = matrix[0].length;
|
||||
int[][] sum = new int[xLength + 1][yLength + 1];
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
int count = 0;
|
||||
for (int i = 1; i < xLength + 1; i++) {
|
||||
for (int j = 1; j < yLength + 1; j++) {
|
||||
sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
|
||||
if (matrix[i - 1][j - 1] == target) {
|
||||
count++;
|
||||
}
|
||||
map.put(sum[i][j], map.getOrDefault(sum[i][j], 0));
|
||||
}
|
||||
}
|
||||
for (int key : map.keySet()) {
|
||||
int value = map.get(key);
|
||||
if (key == 2 * target && value > 1) {
|
||||
count += value * (value - 1) / 2;
|
||||
} else if (key > target && map.containsKey(key - target)) {
|
||||
count += value * map.get(key - target);
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
//total_testcases:40
|
||||
//total_correct:3
|
||||
//input_formatted:[[1,-1],[-1,1]]
|
||||
0
|
||||
//expected_output:5
|
||||
//code_output:0
|
74
src/main/java/leetcode/editor/cn/PowerOfTwo.java
Normal file
74
src/main/java/leetcode/editor/cn/PowerOfTwo.java
Normal file
@ -0,0 +1,74 @@
|
||||
//给你一个整数 n,请你判断该整数是否是 2 的幂次方。如果是,返回 true ;否则,返回 false 。
|
||||
//
|
||||
// 如果存在一个整数 x 使得 n == 2x ,则认为 n 是 2 的幂次方。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:n = 1
|
||||
//输出:true
|
||||
//解释:20 = 1
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:n = 16
|
||||
//输出:true
|
||||
//解释:24 = 16
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:n = 3
|
||||
//输出:false
|
||||
//
|
||||
//
|
||||
// 示例 4:
|
||||
//
|
||||
//
|
||||
//输入:n = 4
|
||||
//输出:true
|
||||
//
|
||||
//
|
||||
// 示例 5:
|
||||
//
|
||||
//
|
||||
//输入:n = 5
|
||||
//输出:false
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// -231 <= n <= 231 - 1
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 进阶:你能够不使用循环/递归解决此问题吗?
|
||||
// Related Topics 位运算 数学
|
||||
// 👍 362 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
//231:2 的幂
|
||||
class PowerOfTwo{
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new PowerOfTwo().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public boolean isPowerOfTwo(int n) {
|
||||
return n > 0 && Integer.bitCount(n) == 1;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
55
src/main/java/leetcode/editor/cn/PowerOfTwo.md
Normal file
55
src/main/java/leetcode/editor/cn/PowerOfTwo.md
Normal file
@ -0,0 +1,55 @@
|
||||
<p>给你一个整数 <code>n</code>,请你判断该整数是否是 2 的幂次方。如果是,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
|
||||
|
||||
<p>如果存在一个整数 <code>x</code> 使得 <code>n == 2<sup>x</sup></code> ,则认为 <code>n</code> 是 2 的幂次方。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 1
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>2<sup>0</sup> = 1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 16
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>2<sup>4</sup> = 16
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 3
|
||||
<strong>输出:</strong>false
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 4
|
||||
<strong>输出:</strong>true
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 5
|
||||
<strong>输出:</strong>false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong>你能够不使用循环/递归解决此问题吗?</p>
|
||||
<div><div>Related Topics</div><div><li>位运算</li><li>数学</li></div></div>\n<div><li>👍 362</li><li>👎 0</li></div>
|
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