Skip to content

Commit b5f356d

Browse files
author
fenghongxiang
committed
2021.02.12 提交若干题目
1 parent 07b1690 commit b5f356d

File tree

8 files changed

+259
-0
lines changed

8 files changed

+259
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
567. 字符串的排列
2+
给定两个字符串 s1 和 s2,写一个函数来判断 s2 是否包含 s1 的排列。
3+
4+
换句话说,第一个字符串的排列之一是第二个字符串的子串。
5+
6+
示例1:
7+
8+
输入: s1 = "ab" s2 = "eidbaooo"
9+
输出: True
10+
解释: s2 包含 s1 的排列之一 ("ba").
11+
12+
13+
示例2:
14+
15+
输入: s1= "ab" s2 = "eidboaoo"
16+
输出: False
17+
18+
19+
注意:
20+
21+
输入的字符串只包含小写字母
22+
两个字符串的长度都在 [1, 10,000] 之间
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package Problem_0567_字符串的排列;
2+
3+
4+
class Solution {
5+
public boolean checkInclusion(String s1, String s2) {
6+
if (s1 == null || s1.isEmpty()) return true;
7+
if (s2 == null || s2.isEmpty()) return false;
8+
if (s1.length() > s2.length()) return false;
9+
10+
int[] dict1 = new int[26];
11+
int[] dict2 = new int[26];
12+
13+
for (int i = 0; i < s1.length(); i++) dict1[s1.charAt(i) - 'a']++;
14+
for (int i = 0; i < s1.length(); i++) dict2[s2.charAt(i) - 'a']++;
15+
16+
Character oldC = null;
17+
18+
for (int i = s1.length() - 1; i < s2.length(); i++) {
19+
if (oldC != null) {
20+
char newC = s2.charAt(i);
21+
dict2[oldC - 'a']--;
22+
dict2[newC - 'a']++;
23+
}
24+
oldC = s2.charAt(i - s1.length() + 1);
25+
26+
boolean flag = true;
27+
for (int m = 0; m < dict1.length; m++) {
28+
if (dict1[m] != dict2[m]) {
29+
flag = false;
30+
break;
31+
}
32+
}
33+
if (flag) return true;
34+
}
35+
return false;
36+
}
37+
}
38+
39+
class Test {
40+
public static void main(String[] args) {
41+
boolean b = new Solution().checkInclusion("abcd", "asoiejtgdacbioaserjgifoj");
42+
System.out.println(b);
43+
}
44+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
对于非负整数 X 而言,X 的数组形式是每位数字按从左到右的顺序形成的数组。例如,如果 X = 1231,那么其数组形式为 [1,2,3,1]
2+
3+
给定非负整数 X 的数组形式 A,返回整数 X+K 的数组形式。
4+
5+
 
6+
7+
示例 1:
8+
9+
输入:A = [1,2,0,0], K = 34
10+
输出:[1,2,3,4]
11+
解释:1200 + 34 = 1234
12+
示例 2:
13+
14+
输入:A = [2,7,4], K = 181
15+
输出:[4,5,5]
16+
解释:274 + 181 = 455
17+
示例 3:
18+
19+
输入:A = [2,1,5], K = 806
20+
输出:[1,0,2,1]
21+
解释:215 + 806 = 1021
22+
示例 4:
23+
24+
输入:A = [9,9,9,9,9,9,9,9,9,9], K = 1
25+
输出:[1,0,0,0,0,0,0,0,0,0,0]
26+
解释:9999999999 + 1 = 10000000000
27+
28+
29+
提示:
30+
31+
1 <= A.length <= 10000
32+
0 <= A[i] <= 9
33+
0 <= K <= 10000
34+
如果 A.length > 1,那么 A[0] != 0
35+
36+
来源:力扣(LeetCode)
37+
链接:https://leetcode-cn.com/problems/add-to-array-form-of-integer
38+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package Problem_0989_数组形式的整数加法;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
class Solution {
7+
public List<Integer> addToArrayForm(int[] A, int K) {
8+
List<Integer> results = new ArrayList<>();
9+
int ALast = A.length;
10+
int Klast = K;
11+
int nextGra = 0;
12+
while (ALast > 0 || Klast > 0 || nextGra > 0) {
13+
int a;
14+
if (ALast > 0) {
15+
a = A[ALast - 1];
16+
} else {
17+
a = 0;
18+
}
19+
int b = Klast % 10;
20+
int c = nextGra % 10;
21+
int result = a + b + c;
22+
ALast--;
23+
Klast /= 10;
24+
c /= 10;
25+
results.add(0, result % 10);
26+
nextGra = result / 10;
27+
}
28+
return results;
29+
}
30+
}
31+
32+
class Test {
33+
public static void main(String[] args) {
34+
List<Integer> integers = new Solution().addToArrayForm(new int[]{9, 9, 9, 9, 9, 9, 9, 9, 9, 9}, 1);
35+
System.out.println(integers);
36+
}
37+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
1603. 设计停车系统
2+
请你给一个停车场设计一个停车系统。停车场总共有三种不同大小的车位:大,中和小,每种尺寸分别有固定数目的车位。
3+
4+
请你实现 ParkingSystem 类:
5+
6+
ParkingSystem(int big, int medium, int small) 初始化 ParkingSystem 类,三个参数分别对应每种停车位的数目。
7+
bool addCar(int carType) 检查是否有 carType 对应的停车位。 carType 有三种类型:大,中,小,分别用数字 1, 2 和 3 表示。一辆车只能停在 carType 对应尺寸的停车位中。如果没有空车位,请返回 false ,否则将该车停入车位并返回 true 。
8+
9+
10+
示例 1:
11+
12+
输入:
13+
["ParkingSystem", "addCar", "addCar", "addCar", "addCar"]
14+
[[1, 1, 0], [1], [2], [3], [1]]
15+
输出:
16+
[null, true, true, false, false]
17+
18+
解释:
19+
ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0);
20+
parkingSystem.addCar(1); // 返回 true ,因为有 1 个空的大车位
21+
parkingSystem.addCar(2); // 返回 true ,因为有 1 个空的中车位
22+
parkingSystem.addCar(3); // 返回 false ,因为没有空的小车位
23+
parkingSystem.addCar(1); // 返回 false ,因为没有空的大车位,唯一一个大车位已经被占据了
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package Problem_1603_设计停车系统;
2+
3+
class ParkingSystem {
4+
5+
int[] array = new int[]{0, 0, 0};
6+
7+
public ParkingSystem(int big, int medium, int small) {
8+
array[0] = big;
9+
array[1] = medium;
10+
array[2] = small;
11+
}
12+
13+
public boolean addCar(int carType) {
14+
if (array[carType - 1] > 0) {
15+
array[carType - 1]--;
16+
return true;
17+
} else return false;
18+
}
19+
}
20+
21+
/**
22+
* Your ParkingSystem object will be instantiated and called as such:
23+
* ParkingSystem obj = new ParkingSystem(big, medium, small);
24+
* boolean param_1 = obj.addCar(carType);
25+
*/
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
5661. 替换隐藏数字得到的最晚时间 显示英文描述
2+
通过的用户数2478
3+
尝试过的用户数2605
4+
用户总通过次数2495
5+
用户总提交次数6534
6+
题目难度Easy
7+
给你一个字符串 time ,格式为 hh:mm(小时:分钟),其中某几位数字被隐藏(用 ? 表示)。
8+
9+
有效的时间为 00:00 到 23:59 之间的所有时间,包括 00:00 和 23:59 。
10+
11+
替换 time 中隐藏的数字,返回你可以得到的最晚有效时间。
12+
13+
14+
15+
示例 1:
16+
17+
输入:time = "2?:?0"
18+
输出:"23:50"
19+
解释:以数字 '2' 开头的最晚一小时是 23 ,以 '0' 结尾的最晚一分钟是 50 。
20+
示例 2:
21+
22+
输入:time = "0?:3?"
23+
输出:"09:39"
24+
示例 3:
25+
26+
输入:time = "1?:22"
27+
输出:"19:22"
28+
29+
30+
提示:
31+
32+
time 的格式为 hh:mm
33+
题目数据保证你可以由输入的字符串生成有效的时间
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package Problem_5661_替换隐藏数字得到的最晚时间;
2+
3+
4+
class Solution {
5+
public String maximumTime(String time) {
6+
String[] timeStr = time.split(":");
7+
if (timeStr[0].equals("??")) {
8+
timeStr[0] = "23";
9+
}
10+
timeStr[0] = timeStr[0].replaceAll("\\?", "9");
11+
timeStr[1] = timeStr[1].replaceAll("\\?", "9");
12+
13+
int hourInt = Integer.parseInt(timeStr[0]);
14+
15+
if (hourInt > 93) {
16+
timeStr[0] = "1" + timeStr[0].charAt(1);
17+
} else if (hourInt >= 90) {
18+
timeStr[0] = "2" + timeStr[0].charAt(1);
19+
} else if (hourInt == 29) {
20+
timeStr[0] = "23";
21+
}
22+
23+
int minuteInt = Integer.parseInt(timeStr[0]);
24+
25+
if (minuteInt >= 90) {
26+
timeStr[1] = "5" + timeStr[1].charAt(1);
27+
}
28+
return timeStr[0] + ":" + timeStr[1];
29+
}
30+
}
31+
32+
class Test {
33+
public static void main(String[] args) {
34+
String s = new Solution().maximumTime("??:3?");
35+
System.out.println(s);
36+
}
37+
}

0 commit comments

Comments
 (0)