Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c2d9a79

Browse files
committed
2023.01.05 andy-pc commit leetcode
0 parents  commit c2d9a79

File tree

212 files changed

+6764
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

212 files changed

+6764
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 2042 检查句子中的数字是否递增
3+
*/
4+
public class AreNumbersAscending {
5+
public boolean areNumbersAscending(String s) {
6+
int pre = 0, cur;
7+
8+
int left=0, right=0;
9+
while(left<s.length()){
10+
char c = s.charAt(left);
11+
12+
if(('0'<=c && c<='9') && ((left==0) ||
13+
(s.charAt(left-1)<'0' || s.charAt(left-1)>'9'))){
14+
right = left+1;
15+
while(right<s.length()){
16+
char t = s.charAt(right);
17+
if(t<'0' || t>'9'){
18+
break;
19+
}else{
20+
right++;
21+
}
22+
}
23+
cur = Integer.parseInt(s.substring(left, right));
24+
if(cur<=pre){
25+
return false;
26+
}else{
27+
pre = cur;
28+
left = right;
29+
}
30+
}else{
31+
left++;
32+
}
33+
}
34+
35+
return true;
36+
}
37+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 1662 检查两个字符串是否相等
3+
*/
4+
5+
public class ArrayStringsAreEqual {
6+
public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
7+
int r1=0,c1=0,r2=0,c2=0;
8+
while (r1<word1.length && r2<word2.length){
9+
if(word1[r1].charAt(c1)!=word2[r2].charAt(c2)) {
10+
return false;
11+
}
12+
c1++;
13+
c2++;
14+
if(c1==word1[r1].length()){
15+
r1++;
16+
c1=0;
17+
}
18+
if(c2==word2[r2].length()){
19+
r2++;
20+
c2=0;
21+
}
22+
}
23+
if(r1==word1.length && r2==word2.length && c1==0 && c2==0){
24+
return true;
25+
}else {
26+
return false;
27+
}
28+
}
29+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 1662 检查两个字符串是否相等
3+
*/
4+
5+
public class ArrayStringsAreEqualRedoI {
6+
public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
7+
int row1=0,col1=0;
8+
int row2=0,col2=0;
9+
while(row1<word1.length && row2<word2.length){
10+
if(col1==word1[row1].length() || col2==word2[row2].length()){
11+
if(col1==word1[row1].length()){
12+
row1++;
13+
col1=0;
14+
}
15+
if(col2==word2[row2].length()){
16+
row2++;
17+
col2=0;
18+
}
19+
}else if(word1[row1].charAt(col1)!=word2[row2].charAt(col2)){
20+
return false;
21+
}else{
22+
col1++;
23+
col2++;
24+
}
25+
}
26+
27+
if(row1==word1.length && row2==word2.length){
28+
return true;
29+
}else{
30+
return false;
31+
}
32+
33+
}
34+
}

leetcode/src/main/java/BuildTree.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.HashMap;
2+
3+
/**
4+
* 105 重建二叉树
5+
*/
6+
7+
public class BuildTree {
8+
public TreeNode buildTree(int[] preorder, int[] inorder) {
9+
if(preorder.length!=inorder.length || preorder.length==0){
10+
return null;
11+
}
12+
HashMap<Integer, Integer> map = new HashMap<>();
13+
for(int i=0; i<inorder.length; i++){
14+
map.put(inorder[i], i);
15+
}
16+
TreeNode root = dp(0, 0, inorder.length-1, preorder, map);
17+
return root;
18+
}
19+
20+
TreeNode dp(int pos, int left, int right, int[] preorder, HashMap<Integer, Integer> map){
21+
// 递归终止
22+
if(left>right){
23+
return null;
24+
}
25+
26+
// 建立根节点
27+
TreeNode node = new TreeNode(preorder[pos]);
28+
// 划分根节点、左子树、右子树
29+
int i = map.get(preorder[pos]);
30+
// 开启左子树递归
31+
node.left = dp(pos+1, left, i-1, preorder, map);
32+
// 开启右子树递归
33+
node.right = dp(pos+i-left+1, i+1, right, preorder, map);
34+
35+
return node;
36+
}
37+
}

leetcode/src/main/java/CanJump.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* 55 跳跃游戏
3+
*/
4+
5+
public class CanJump {
6+
public boolean canJump(int[] nums) {
7+
boolean res = true;
8+
int k = 0;
9+
for(int i=0; i<nums.length; i++) {
10+
if(i>k) {
11+
return false;
12+
}
13+
k = Math.max(k, i+nums[i]);
14+
}
15+
16+
return res;
17+
}
18+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import java.util.HashMap;
2+
3+
/**
4+
* 567. 字符串的排列
5+
*/
6+
7+
public class CheckInclusion {
8+
9+
public boolean checkInclusion(String s1, String s2) {
10+
int count = 0;
11+
int left=0, right=0;
12+
HashMap<Character, Integer> map1 = new HashMap<>();
13+
HashMap<Character, Integer> map2 = new HashMap<>();
14+
for(int i=0; i<s1.length(); i++){
15+
char c1 = s1.charAt(i);
16+
map1.put(c1, map1.getOrDefault(c1, 0)+1);
17+
}
18+
19+
for(int i=0; i<s1.length(); i++){
20+
char c = s2.charAt(i);
21+
map2.put(c, map2.getOrDefault(c, 0)+1);
22+
if(map2.get(c)<=map1.getOrDefault(c,0)){
23+
count++;
24+
}
25+
right++;
26+
}
27+
if(count==s1.length()){
28+
return true;
29+
}
30+
31+
while(right<s2.length()){
32+
char c1 = s2.charAt(left);
33+
char c2 = s2.charAt(right);
34+
map2.put(c2, map2.getOrDefault(c2, 0)+1);
35+
if(map2.get(c2)<=map1.getOrDefault(c2, 0)){
36+
count++;
37+
}
38+
map2.put(c1, map2.get(c1)-1);
39+
if(map2.get(c1)<map1.getOrDefault(c1, 0)){
40+
count--;
41+
}
42+
43+
left++;
44+
right++;
45+
if(count==s1.length()){
46+
return true;
47+
}
48+
}
49+
50+
51+
return false;
52+
}
53+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.Stack;
2+
3+
/**
4+
* 1784. 检查二进制字符串字段
5+
*/
6+
7+
public class CheckOnesSegment {
8+
public boolean checkOnesSegment(String s) {
9+
boolean res = true;
10+
Stack<Character> stack = new Stack<>();
11+
for(char c: s.toCharArray()){
12+
if(stack.isEmpty()){
13+
stack.push(c);
14+
}else{
15+
if(stack.peek()==c){
16+
continue;
17+
}else if(stack.size()==1){
18+
stack.push(c);
19+
}else if(stack.size()>1 && stack.peek()!=c){
20+
return false;
21+
}
22+
}
23+
}
24+
25+
return res;
26+
}
27+
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
/**
5+
* 39. 数组总和
6+
*/
7+
8+
public class CombinationSum {
9+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
10+
List<List<Integer>> res = new ArrayList<>();
11+
dp(0, candidates, target, new ArrayList<Integer>(), res);
12+
13+
return res;
14+
}
15+
16+
void dp(int start, int[] candidates, int target, ArrayList<Integer> temp, List<List<Integer>> res){
17+
if(target==0){
18+
res.add(new ArrayList<>(temp));
19+
}else if(target<0){
20+
return;
21+
}
22+
for(int i=start; i<candidates.length; i++){
23+
temp.add(candidates[i]);
24+
dp(i, candidates, target-candidates[i], temp, res);
25+
temp.remove(temp.size()-1);
26+
}
27+
}
28+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.List;
4+
5+
/**
6+
* 39. 数组总和
7+
*/
8+
9+
public class CombinationSum2 {
10+
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
11+
List<List<Integer>> res = new ArrayList<>();
12+
Arrays.sort(candidates); // 从小到大排序
13+
dp(0, candidates, target, new ArrayList<Integer>(), res);
14+
15+
return res;
16+
}
17+
18+
void dp(int start, int[] candidates, int target, ArrayList<Integer> temp, List<List<Integer>> res){
19+
if(target==0){
20+
res.add(new ArrayList<>(temp));
21+
}else if(target<0){
22+
return;
23+
}
24+
for(int i=start; i<candidates.length; i++){
25+
// 枝剪去重
26+
if(i>start && candidates[i]==candidates[i-1]){
27+
continue;
28+
}
29+
temp.add(candidates[i]);
30+
dp(i+1, candidates, target-candidates[i], temp, res);
31+
temp.remove(temp.size()-1);
32+
}
33+
}
34+
}

leetcode/src/main/java/Combine.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
/**
5+
* 77. 组合
6+
*/
7+
8+
public class Combine {
9+
public List<List<Integer>> combine(int n, int k) {
10+
List<List<Integer>> res = new ArrayList<>();
11+
12+
backtrack(k, 1, n, new ArrayList<Integer>(), res);
13+
return res;
14+
}
15+
16+
void backtrack(int k, int start, int n, List<Integer> temp, List<List<Integer>> res){
17+
if(k==0){
18+
res.add(new ArrayList<>(temp));
19+
}
20+
for(int i=start; i<=n; i++){
21+
temp.add(i);
22+
backtrack(k-1, i+1, n, temp, res);
23+
temp.remove(temp.size()-1);
24+
}
25+
}
26+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.ArrayList;
2+
import java.util.HashMap;
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
/**
7+
* 1002. 查找共用字符
8+
*/
9+
10+
public class CommonChars {
11+
public List<String> commonChars(String[] words) {
12+
HashMap<Character, Integer> map = new HashMap<>();
13+
HashMap<Character, Integer> temp = new HashMap<>();
14+
List<String> res = new ArrayList<>();
15+
for(char c: words[0].toCharArray()){
16+
temp.put(c, temp.getOrDefault(c, 0)+1);
17+
}
18+
for(int i=1; i<words.length; i++){
19+
map = temp;
20+
temp = new HashMap<>();
21+
for(char c: words[i].toCharArray()){
22+
if(map.getOrDefault(c, 0)>0){
23+
map.put(c, map.get(c)-1);
24+
temp.put(c, temp.getOrDefault(c, 0)+1);
25+
}
26+
}
27+
}
28+
29+
for(Map.Entry<Character, Integer> t: temp.entrySet()){
30+
for(int i=0; i<t.getValue(); i++){
31+
res.add(t.getKey().toString());
32+
}
33+
}
34+
35+
return res;
36+
}
37+
}

0 commit comments

Comments
 (0)