Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4e09d85

Browse files
committed
clean code
1 parent 24a2428 commit 4e09d85

File tree

19 files changed

+322
-67
lines changed

19 files changed

+322
-67
lines changed

‎codeforces/codeforces-19/src/main/java/p1842/CF1842C.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ private static String solve() {
4848
C. Tenzing and Balls
4949
https://codeforces.com/contest/1842/problem/C
5050
51+
灵茶の试炼 2024年12月17日
5152
题目大意:
5253
Tenzing 有 n 个球排成一行。左边第 i 个球的颜色是 ai。
5354
Tenzing 可以任意次数做以下操作:

‎codeforces/codeforces-19/src/main/java/p1878/CF1878E.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ static boolean checkMid(int l, int k, int mid) {
7878
E. Iva & Pav
7979
https://codeforces.com/contest/1878/problem/E
8080
81+
灵茶の试炼 2025年02月04日
8182
题目大意:
8283
Iva 和 Pav 是一对著名的塞尔维亚竞技编程夫妇。在塞尔维亚,人们称帕夫为"帕普卡",这就是为什么他会让伊娃的所有愿望都实现。
8384
Iva 给了 Pav 一个 n 个元素的数组 a。

‎codeforces/codeforces-20/src/main/java/p1932/CF1932C.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ private static String solve() {
4747
C. LR-remainders
4848
https://codeforces.com/contest/1932/problem/C
4949
50+
灵茶の试炼 2025年01月28日
5051
题目大意:
5152
给定一个长度为 n 的数组 a,一个正整数 m,以及一串长度为 n 的命令。每个命令都是字符"L"或字符"R"。
5253
按照在字符串 s 中写入的顺序处理所有 n 个命令。命令的处理方法如下:

‎leetcode/leetcode-01/src/main/java/Solution12.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ public class Solution12 {
33
private static final String[] SYMBOLS = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
44

55
public String intToRoman(int num) {
6-
StringBuilder stringBuilder = new StringBuilder();
6+
StringBuilder ans = new StringBuilder();
77
for (int i = 0; i < VALUES.length; i++) {
88
int value = VALUES[i];
99
String symbol = SYMBOLS[i];
1010
while (num - value >= 0) {
1111
num -= value;
12-
stringBuilder.append(symbol);
12+
ans.append(symbol);
1313
}
1414
if (num == 0) {
1515
break;
1616
}
1717
}
18-
return stringBuilder.toString();
18+
return ans.toString();
1919
}
2020
}
2121
/*
Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,63 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.Comparator;
4+
import java.util.List;
5+
16
public class Solution1534 {
7+
// O(n^3)
28
public int countGoodTriplets(int[] arr, int a, int b, int c) {
3-
int len = arr.length;
4-
int cnt = 0;
5-
for (int i = 0; i < len; i++) {
6-
for (int j = i + 1; j < len; j++) {
7-
if ((Math.abs(arr[i] - arr[j]) <= a)) {
8-
cnt = getCnt(arr, b, c, len, cnt, i, j);
9+
int n = arr.length;
10+
int ans = 0;
11+
for (int i = 0; i < n; i++) {
12+
for (int j = i + 1; j < n; j++) {
13+
if ((Math.abs(arr[i] - arr[j]) > a)) continue;
14+
for (int k = j + 1; k < n; k++) {
15+
if ((Math.abs(arr[j] - arr[k]) <= b) && (Math.abs(arr[i] - arr[k]) <= c)) {
16+
ans++;
17+
}
918
}
1019
}
1120
}
12-
return cnt;
21+
return ans;
1322
}
1423

15-
private int getCnt(int[] arr, int b, int c, int len, int cnt, int i, int j) {
16-
for (int k = j + 1; k < len; k++) {
17-
if ((Math.abs(arr[j] - arr[k]) <= b) && (Math.abs(arr[i] - arr[k]) <= c)) {
18-
cnt++;
24+
// O(n^2)
25+
public int countGoodTriplets2(int[] arr, int a, int b, int c) {
26+
int n = arr.length;
27+
Integer[] ids = new Integer[n];
28+
for (int i = 0; i < n; i++) ids[i] = i;
29+
Arrays.sort(ids, Comparator.comparingInt(o -> arr[o]));
30+
31+
int ans = 0;
32+
for (int j : ids) {
33+
int y = arr[j];
34+
List<Integer> left = new ArrayList<>();
35+
for (int i : ids) {
36+
if (i < j && Math.abs(arr[i] - y) <= a) {
37+
left.add(arr[i]);
38+
}
39+
}
40+
41+
List<Integer> right = new ArrayList<>();
42+
for (int k : ids) {
43+
if (k > j && Math.abs(arr[k] - y) <= b) {
44+
right.add(arr[k]);
45+
}
46+
}
47+
48+
int k1 = 0;
49+
int k2 = 0;
50+
for (int x : left) {
51+
while (k2 < right.size() && right.get(k2) <= x + c) {
52+
k2++;
53+
}
54+
while (k1 < right.size() && right.get(k1) < x - c) {
55+
k1++;
56+
}
57+
ans += k2 - k1;
1958
}
2059
}
21-
return cnt;
60+
return ans;
2261
}
2362
}
2463
/*
@@ -40,7 +79,6 @@ private int getCnt(int[] arr, int b, int c, int len, int cnt, int i, int j) {
4079
0 <= arr[i] <= 1000
4180
0 <= a, b, c <= 1000
4281
43-
暴力枚举即可。时间复杂度 O(n^3)
44-
可以进行剪枝,时间复杂度 O(n^2)
45-
抽取方法避免嵌套过深。
82+
暴力枚举 / 前缀和 / 排序+三指针
83+
https://leetcode.cn/problems/count-good-triplets/solutions/3622921/liang-chong-fang-fa-bao-li-mei-ju-qian-z-apcv/
4684
*/

‎leetcode/leetcode-16/src/test/java/Solution1534Tests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public void example1() {
1212
int c = 3;
1313
int expected = 4;
1414
Assertions.assertEquals(expected, solution1534.countGoodTriplets(arr, a, b, c));
15+
Assertions.assertEquals(expected, solution1534.countGoodTriplets2(arr, a, b, c));
1516
}
1617

1718
@Test
@@ -22,5 +23,6 @@ public void example2() {
2223
int c = 1;
2324
int expected = 0;
2425
Assertions.assertEquals(expected, solution1534.countGoodTriplets(arr, a, b, c));
26+
Assertions.assertEquals(expected, solution1534.countGoodTriplets2(arr, a, b, c));
2527
}
2628
}

‎leetcode/leetcode-19/src/main/java/Solution1863.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ private void helper(int[] nums, int idx, LinkedList<Integer> subset, List<List<I
3838
subset.removeLast();
3939
}
4040
}
41+
42+
// O(n)
43+
public int subsetXORSum2(int[] nums) {
44+
int n = nums.length;
45+
int or = 0;
46+
for (int x : nums) {
47+
or |= x;
48+
}
49+
return or << (n - 1);
50+
}
4151
}
4252
/*
4353
1863. 找出所有子集的异或总和再求和

‎leetcode/leetcode-19/src/test/java/Solution1863Tests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@ public void example1() {
99
int[] nums = {1, 3};
1010
int expected = 6;
1111
Assertions.assertEquals(expected, solution1863.subsetXORSum(nums));
12+
Assertions.assertEquals(expected, solution1863.subsetXORSum2(nums));
1213
}
1314

1415
@Test
1516
public void example2() {
1617
int[] nums = {5, 1, 6};
1718
int expected = 28;
1819
Assertions.assertEquals(expected, solution1863.subsetXORSum(nums));
20+
Assertions.assertEquals(expected, solution1863.subsetXORSum2(nums));
1921
}
2022

2123
@Test
2224
public void example3() {
2325
int[] nums = {3, 4, 5, 6, 7, 8};
2426
int expected = 480;
2527
Assertions.assertEquals(expected, solution1863.subsetXORSum(nums));
28+
Assertions.assertEquals(expected, solution1863.subsetXORSum2(nums));
2629
}
2730
}

‎leetcode/leetcode-21/src/main/java/Solution2075.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
public class Solution2075 {
22
public String decodeCiphertext(String encodedText, int rows) {
3-
int len = encodedText.length();
4-
int cols = len / rows;
3+
int cols = encodedText.length() / rows;
54
// 还原矩阵
6-
char[][] chars = new char[rows][cols];
5+
char[][] grid = new char[rows][cols];
76
int idx = 0;
87
for (int i = 0; i < rows; i++) {
98
for (int j = 0; j < cols; j++) {
10-
chars[i][j] = encodedText.charAt(idx++);
9+
grid[i][j] = encodedText.charAt(idx++);
1110
}
1211
}
1312
// 还原 originalText
14-
StringBuilder stringBuilder = new StringBuilder();
13+
StringBuilder ans = new StringBuilder();
1514
for (int i = 0; i < cols; i++) {
1615
for (int j = 0; j < rows; j++) {
1716
if (j + i < cols) {
18-
stringBuilder.append(chars[j][j + i]);
17+
ans.append(grid[j][j + i]);
1918
}
2019
}
2120
}
2221
// 移除 尾随空格
23-
return stringBuilder.toString().stripTrailing();
22+
return ans.toString().stripTrailing();
2423
}
2524
}
2625
/*
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
public class Solution2109 {
22
public String addSpaces(String s, int[] spaces) {
3-
int first = spaces[0];
4-
int last = spaces[spaces.length - 1];
5-
StringBuilder stringBuilder = new StringBuilder();
6-
7-
// first
8-
stringBuilder.append(s, 0, first).append(" ");
9-
// middle
10-
for (int i = 1; i < spaces.length; i++) {
11-
stringBuilder.append(s, spaces[i - 1], spaces[i]).append(" ");
3+
int n = spaces.length;
4+
StringBuilder ans = new StringBuilder();
5+
ans.append(s, 0, spaces[0]).append(" "); // 也可以加 0 和 s.length() 两个哨兵
6+
for (int i = 1; i < n; i++) {
7+
ans.append(s, spaces[i - 1], spaces[i]).append(" ");
128
}
13-
// last
14-
stringBuilder.append(s, last, s.length());
15-
return stringBuilder.toString();
9+
ans.append(s, spaces[n - 1], s.length());
10+
return ans.toString();
1611
}
1712
}
1813
/*
@@ -27,4 +22,5 @@ public String addSpaces(String s, int[] spaces) {
2722
请你添加空格,并返回修改后的字符串。
2823
2924
java 字符串是不可变类型,因此不用考虑在原字符串上插入空格导致下标变化问题,直接新开一个 StringBuilder 模拟即可。
25+
StringBuilder#append(CharSequence s, int start, int end) 等价于 StringBuilder#append(s[start:end])
3026
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /