This commit is contained in:
fleyx 2024-01-24 22:22:44 +08:00
parent 007a62df51
commit 3c231d2345
11 changed files with 350 additions and 1 deletions

View File

@ -0,0 +1,26 @@
package com.fanxb.common;
/**
* Created with IntelliJ IDEA
*
* @author fanxb
* Date: 2020/6/10 10:49
*/
public class Q11 {
public int maxArea(int[] height) {
int length = height.length;
int max = 0, i = 0, j = length - 1;
while (i < j) {
int temp = (j - i) * Math.min(height[i], height[j]);
if (max < temp) max = temp;
if (height[i] > height[j]) j--;
else i++;
}
return max;
}
public static void main(String[] args) {
Q11 instance = new Q11();
}
}

View File

@ -0,0 +1,42 @@
package com.fanxb.common;
import java.util.*;
import java.util.stream.Stream;
/**
* Created with IntelliJ IDEA
*
* @author fanxb
* Date: 2020/6/10 10:49
*/
public class Q15 {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
Set<String> has = new HashSet<>();
int length = nums.length;
for (int i = 0; i < length - 2; i++) {
if (nums[i] > 0) {
return res;
}
int l = i + 1, r = length - 1;
while (l < r) {
int sum = nums[i] + nums[l] + nums[r];
if (sum == 0) {
String key = nums[i] + "," + nums[l] + "," + nums[r];
if (!has.contains(key)) {
res.add(new ArrayList<>(Arrays.asList(nums[i], nums[l], nums[r])));
has.add(key);
}
break;
} else if (sum > 0) r--;
else l++;
}
}
return res;
}
public static void main(String[] args) {
Q15 instance = new Q15();
}
}

View File

@ -0,0 +1,38 @@
package com.fanxb.common;
public class Q150 {
public void gameOfLife(int[][] board) {
int m = board.length, n = board[0].length;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int aliveCount = 0;
if (i > 0 && j > 0 && board[i - 1][j - 1] >= 1) aliveCount++;
if (i > 0 && board[i - 1][j] >= 1) aliveCount++;
if (i > 0 && j < n - 1 && board[i - 1][j + 1] >= 1) aliveCount++;
if (j > 0 && board[i][j - 1] >= 1) aliveCount++;
if (j < m - 1 && board[i][j + 1] >= 1) aliveCount++;
if (i < m - 1 && j > 0 && board[i + 1][j - 1] >= 1) aliveCount++;
if (i < m - 1 && board[i + 1][j] >= 1) aliveCount++;
if (i < m - 1 && j < n - 1 && board[i + 1][j + 1] >= 1) aliveCount++;
if (board[i][j] == 0) {
//死到活 -1
if (aliveCount == 3) board[i][j] = -1;
} else {
//活到死 2
if (aliveCount < 2 || aliveCount > 3) board[i][j] = 2;
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] == -1) board[i][j] = 1;
else if (board[i][j] == 2) board[i][j] = 0;
}
}
}
public static void main(String[] args) {
System.out.println(new Q150().maxPoints(new int[][]{{1, 1}, {3, 2}, {5, 3}, {4, 1}, {2, 3}, {1, 4}}));
}
}

View File

@ -1,6 +1,7 @@
package com.fanxb.common;
import java.util.Arrays;
import java.util.EnumSet;
/**
* 两数之和 II - 输入有序数组
@ -28,6 +29,16 @@ public class Q167 {
return new int[]{i + 1, j + 1};
}
public int[] twoSum(int[] numbers, int target) {
int i = 0, j = numbers.length - 1;
while (i < j) {
int sum = numbers[i] + numbers[j];
if (sum == target) return new int[]{i+1, j+1};
else if (sum > target) j--;
else j++;
}
return null;
}
public static void main(String[] args) {
int[] numbers = {2, 7, 11, 15};

View File

@ -0,0 +1,22 @@
package com.fanxb.common;
import java.util.Arrays;
public class Q205 {
public boolean isIsomorphic(String s, String t) {
int[] map = new int[128];
Arrays.fill(map, -1);
int length = s.length();
for (int i = 0; i < length; i++) {
char tc = t.charAt(i), sc = s.charAt(i);
if (map[tc] == -1) map[tc] = sc;
if (map[tc] != sc) return false;
}
return true;
}
public static void main(String[] args) {
int[] s = {1, 2, 3, 1};
}
}

View File

@ -0,0 +1,34 @@
package com.fanxb.common;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Created with IntelliJ IDEA
*
* @author fanxb
* Date: 2020/6/11 9:56
*/
public class Q290 {
public boolean wordPattern(String pattern, String s) {
List<String> arr = Stream.of(s.split(" ")).filter(item -> !item.isEmpty()).collect(Collectors.toList());
if (arr.size() != pattern.length()) return false;
String[] map = new String[128];
Map<String, Character> map1 = new HashMap<>(arr.size());
for (int i = 0; i < pattern.length(); i++) {
char c = pattern.charAt(i);
String str = arr.get(i);
if (map[c] == null) map[c] = str;
map1.putIfAbsent(str, c);
if (!map[c].equals(str) || !map1.get(str).equals(c)) return false;
}
return true;
}
public static void main(String[] args) {
System.out.println(new Q290().firstBadVersion(16));
}
}

View File

@ -0,0 +1,56 @@
package com.fanxb.common;
import java.util.Arrays;
/**
*
*/
public class Q36 {
public boolean isValidSudoku(char[][] board) {
boolean[] check = new boolean[10];
char c;
for (int i = 0; i < 9; i++) {
//检查行
for (int j = 0; j < 9; j++) {
c = board[i][j];
if (c != '.') {
int num = c - '0';
if (check[num]) return false;
check[num] = true;
}
}
Arrays.fill(check, false);
//检查列
for (int j = 0; j < 9; j++) {
c = board[j][i];
if (c != '.') {
int num = c - '0';
if (check[num]) return false;
check[num] = true;
}
}
Arrays.fill(check, false);
//检查小9宫格
int starti = 3 * (i / 3), startj = (i % 3) * 3;
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
c = board[starti + x][startj + y];
if (c != '.') {
int num = c - '0';
if (check[num]) return false;
check[num] = true;
}
}
}
Arrays.fill(check, false);
}
return true;
}
public static void main(String[] args) {
char[][] chars =
new char[][]{{'5', '3', '.', '.', '7', '.', '.', '.', '.'}, {'6', '.', '.', '1', '9', '5', '.', '.', '.'}, {'.', '9', '8', '.', '.', '.', '.', '6', '.'}, {'8', '.', '.', '.', '6', '.', '.', '.', '3'}, {'4', '.', '.', '8', '.', '3', '.', '.', '1'}, {'7', '.', '.', '.', '2', '.', '.', '.', '6'}, {'.', '6', '.', '.', '.', '.', '2', '8', '.'}, {'.', '.', '.', '4', '1', '9', '.', '.', '5'}, {'.', '.', '.', '.', '8', '.', '.', '7', '9'}};
System.out.println(new Q36().isValidSudoku(chars));
}
}

View File

@ -0,0 +1,26 @@
package com.fanxb.common;
/**
* 两数相加
*
* @author fanxb
* @date 2021/6/1
**/
public class Q383 {
public boolean canConstruct(String ransomNote, String magazine) {
int[] charCount = new int[128];
for (int i = 0; i < magazine.length(); i++) {
charCount[magazine.charAt(i)]++;
}
for (int i = 0; i < ransomNote.length(); i++) {
char c = ransomNote.charAt(i);
if (charCount[c] == 0)
return false;
charCount[c]--;
}
return true;
}
public static void main(String[] args) {
}
}

View File

@ -0,0 +1,35 @@
package com.fanxb.common;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
public class Q48 {
public void rotate(int[][] matrix) {
int length = matrix.length;
for (int i = 0; i < length / 2; i++) {
int length1 = length - i * 2;
for (int j = 0; j < length1-1; j++) {
int temp = matrix[i + j][i + length1 - 1];
matrix[i + j][i + length1 - 1] = matrix[i][i + j];
int temp1 = matrix[i + length1 - 1][i + length1 - 1 - j];
matrix[i + length1 - 1][i + length1 - 1 - j] = temp;
temp = matrix[i+length1-1-j][i];
matrix[i+length1-1-j][i] = temp1;
matrix[i][i + j] = temp;
}
}
}
public static void main(String[] args) {
int[][] arr = new int[][]{{5,1,9,11},{2,4,8,10},{13,3,6,7},{15,14,12,16}};
Q48 ins = new Q48();
ins.rotate(arr);
for (int[] temp : arr) {
System.out.println(Arrays.toString(temp));
}
}
}

View File

@ -0,0 +1,29 @@
package com.fanxb.common;
import java.util.Arrays;
public class Q73 {
public void setZeroes(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
//行是否有0
int[] cache1 = new int[m];
//列是否有0
int[] cache2 = new int[n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (matrix[i][j] == 0) {
cache1[i] = 1;
cache2[j] = 1;
}
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (cache1[i] == 1 || cache2[j] == 1)
matrix[i][j] = 0;
}
public static void main(String[] args) {
System.out.println(new Q73().minDistance("horse", "ros"));
}
}

View File

@ -51,6 +51,36 @@ public class Q76 {
return start < 0 ? "" : s.substring(start, end + 1);
}
public String minWindow(String s, String t) {
int sSize = s.length(), needCount = t.length();
int[] map = new int[128];
for (int i = 0; i < needCount; i++) map[t.charAt(i)]++;
int l = 0, r = 0, minL = 0, minR = sSize;
while (r < sSize) {
char temp = s.charAt(r);
if (map[temp] > 0) needCount--;
map[temp]--;
if (needCount == 0) {
//说明包含了所有元素,开始把l向右移动直到刚好包含所有元素
while (needCount == 0) {
temp = s.charAt(l);
if (map[temp] == 0) {
//说明l这个位置刚好
if ((r - l) < (minR - minL)) {
minL = l;
minR = r;
}
needCount++;
}
map[temp]++;
l++;
}
}
r++;
}
return minR == sSize ? "" : s.substring(minL, minR + 1);
}
public static void main(String[] args) {
System.out.println(solution("a", "aa"));