Skip to content

Commit fddce3c

Browse files
committed
LeetCode 题解 1554~1569
1 parent 8fcb75b commit fddce3c

File tree

12 files changed

+270
-0
lines changed

12 files changed

+270
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public String makeGood(String s) {
3+
return help(s);
4+
}
5+
6+
private String help(String s) {
7+
if (s == null || s.length() == 0 || s.length() == 1) {
8+
return s;
9+
}
10+
char[] chars = s.toCharArray();
11+
for (int i = 1; i < chars.length; i++) {
12+
if (Math.abs(chars[i] - chars[i - 1]) == Math.abs('A' - 'a')) {
13+
return help(newString(chars, i));
14+
}
15+
}
16+
return s;
17+
}
18+
19+
private String newString(char[] chars, int i) {
20+
StringBuilder tmp = new StringBuilder();
21+
for (int j = 0; j < chars.length; j++) {
22+
if (j != i && j != i - 1) {
23+
tmp.append(chars[j]);
24+
}
25+
}
26+
return tmp.toString();
27+
}
28+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public char findKthBit(int n, int k) {
3+
if (k == 1 || n == 1) {
4+
return '0';
5+
}
6+
Set<Integer> set = new HashSet<>();
7+
int len = calcLength(n, set);
8+
if (set.contains(k)) {
9+
return '1';
10+
}
11+
// 中间,返回1
12+
if (k < len / 2) {
13+
return findKthBit(n - 1, k);
14+
} else {
15+
if (set.contains(len - k)) {
16+
return '1';
17+
}
18+
return r(findKthBit(n - 1, len - k + 1));
19+
}
20+
}
21+
22+
private char r(char b) {
23+
if (b == '0') {
24+
return '1';
25+
}
26+
return '0';
27+
}
28+
29+
private int calcLength(int n, Set<Integer> set) {
30+
if (n == 1) {
31+
return 1;
32+
}
33+
34+
int ans = 2 * calcLength(n - 1, set) + 1;
35+
set.add(ans + 1);
36+
return ans;
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int maxNonOverlapping(int[] nums, int target) {
3+
Set<Integer> set = new HashSet<>();
4+
set.add(0);
5+
int sum = 0, ans = 0;
6+
for (int num : nums) {
7+
sum += num;
8+
if (set.contains(sum - target)) {
9+
ans++;
10+
set.clear();
11+
sum = 0;
12+
}
13+
14+
set.add(sum);
15+
}
16+
return ans;
17+
}
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public boolean threeConsecutiveOdds(int[] arr) {
3+
int count = 0;
4+
for (int n : arr) {
5+
if (n % 2 == 0) {
6+
count = 0;
7+
} else {
8+
count++;
9+
if (count >= 3) {
10+
return true;
11+
}
12+
}
13+
}
14+
return false;
15+
}
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution {
2+
public int minOperations(int n) {
3+
int ans = 0;
4+
for (int i = 0; i < n / 2; i++) {
5+
int curr = 2 * i + 1;
6+
ans += Math.abs(n - curr);
7+
}
8+
return ans;
9+
}
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
private Map<Integer, Integer> map = new HashMap<>();
3+
4+
public int minDays(int n) {
5+
if (n < 2) {
6+
return n;
7+
}
8+
if (!map.containsKey(n)) {
9+
map.put(n, Math.min(minDays(n / 2) + 1 + n % 2, minDays(n / 3) + 1 + n % 3));
10+
}
11+
return map.get(n);
12+
}
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public List<Integer> mostVisited(int n, int[] rounds) {
3+
int[] ans = new int[n];
4+
for (int i = 0; i < rounds.length; i++) {
5+
rounds[i]--;
6+
}
7+
ans[rounds[0]]++;
8+
for (int i = 0; i < rounds.length - 1; i++) {
9+
int start = rounds[i];
10+
int end = rounds[i + 1];
11+
if (end <= start) {
12+
end += n;
13+
}
14+
for (int j = start + 1; j <= end; j++) {
15+
ans[j % n]++;
16+
}
17+
}
18+
19+
int max = Arrays.stream(ans).max().orElse(-1);
20+
List<Integer> list = new ArrayList<>();
21+
for (int i = 0; i < ans.length; i++) {
22+
if (ans[i] == max) {
23+
list.add(i + 1);
24+
}
25+
}
26+
return list;
27+
}
28+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int maxCoins(int[] piles) {
3+
Arrays.sort(piles);
4+
int start = 0, end = piles.length - 1, ans = 0;
5+
while (start < end) {
6+
ans += piles[end - 1];
7+
start++;
8+
end -= 2;
9+
}
10+
return ans;
11+
}
12+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int findLatestStep(int[] arr, int m) {
3+
// 倒序遍历 arr,转换为第一次出现 m 个的步骤
4+
if (arr.length == m) {
5+
return m;
6+
}
7+
TreeSet<Integer> set = new TreeSet<>();
8+
set.add(0);
9+
set.add(arr.length + 1);
10+
for (int i = arr.length - 1; i >= 0; i--) {
11+
int index = arr[i];
12+
int l = set.lower(index);
13+
int h = set.higher(index);
14+
if (index - l - 1 == m || h - index - 1 == m) {
15+
return i;
16+
}
17+
set.add(index);
18+
}
19+
return -1;
20+
}
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public boolean containsPattern(int[] arr, int m, int k) {
3+
if (arr.length < m * k) {
4+
return false;
5+
}
6+
for (int i = 0; i <= arr.length - m * k; i++) {
7+
boolean match = true;
8+
for (int j = i + m; j < i + m * k; j++) {
9+
if (arr[j] != arr[j - m]) {
10+
match = false;
11+
break;
12+
}
13+
}
14+
if (match) {
15+
return true;
16+
}
17+
}
18+
return false;
19+
}
20+
}

0 commit comments

Comments
 (0)