Skip to content

Commit 4215fe7

Browse files
committed
牛客周赛Round 1-2-11-12-13
1 parent dc7c3c9 commit 4215fe7

File tree

105 files changed

+2401
-17
lines changed

Some content is hidden

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

105 files changed

+2401
-17
lines changed

jacoco-aggregate-report/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,11 @@
286286
<artifactId>leetcode-29</artifactId>
287287
<version>1.0-SNAPSHOT</version>
288288
</dependency>
289+
<dependency>
290+
<groupId>com.devyy</groupId>
291+
<artifactId>leetcode-30</artifactId>
292+
<version>1.0-SNAPSHOT</version>
293+
</dependency>
289294

290295
<dependency>
291296
<groupId>com.devyy</groupId>
@@ -307,6 +312,13 @@
307312
<artifactId>leetcode-offer</artifactId>
308313
<version>1.0-SNAPSHOT</version>
309314
</dependency>
315+
316+
317+
<dependency>
318+
<groupId>com.devyy</groupId>
319+
<artifactId>nowcoder</artifactId>
320+
<version>1.0-SNAPSHOT</version>
321+
</dependency>
310322
</dependencies>
311323

312324
<build>

leetcode/leetcode-03/src/main/java/Solution238.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
public class Solution238 {
22
public int[] productExceptSelf(int[] nums) {
3-
int len = nums.length;
4-
int[] leftProduct = new int[len];
5-
int[] rightProduct = new int[len];
6-
leftProduct[0] = 1;
7-
for (int i = 1; i < len; i++) {
8-
leftProduct[i] = leftProduct[i - 1] * nums[i - 1];
3+
int n = nums.length;
4+
5+
int[] L = new int[n];
6+
int[] R = new int[n];
7+
L[0] = 1;
8+
for (int i = 1; i < n; i++) {
9+
L[i] = L[i - 1] * nums[i - 1];
910
}
10-
rightProduct[len - 1] = 1;
11-
for (int i = len - 2; i >= 0; i--) {
12-
rightProduct[i] = rightProduct[i + 1] * nums[i + 1];
11+
R[n - 1] = 1;
12+
for (int i = n - 2; i >= 0; i--) {
13+
R[i] = R[i + 1] * nums[i + 1];
1314
}
14-
int[] res = new int[len];
15-
for (int i = 0; i < len; i++) {
16-
res[i] = leftProduct[i] * rightProduct[i];
15+
16+
int[] ans = new int[n];
17+
for (int i = 0; i < n; i++) {
18+
ans[i] = L[i] * R[i];
1719
}
18-
return res;
20+
return ans;
1921
}
2022
}
2123
/*

leetcode/leetcode-29/src/main/java/Solution2873.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ public class Solution2873 {
1313
3 <= nums.length <= 100
1414
1 <= nums[i] <= 10^6
1515
16-
同 2874. 有序三元组中的最大值 II
16+
: 2874. 有序三元组中的最大值 II
1717
https://leetcode.cn/problems/maximum-value-of-an-ordered-triplet-ii/description/
1818
*/

leetcode/leetcode-30/pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.devyy</groupId>
8+
<artifactId>leetcode</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>leetcode-30</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>17</maven.compiler.source>
16+
<maven.compiler.target>17</maven.compiler.target>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>com.devyy</groupId>
23+
<artifactId>leetcode-core</artifactId>
24+
<version>1.0-SNAPSHOT</version>
25+
</dependency>
26+
</dependencies>
27+
</project>

leetcode/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<module>leetcode-27</module>
4242
<module>leetcode-28</module>
4343
<module>leetcode-29</module>
44+
<module>leetcode-30</module>
4445

4546
<module>leetcode-core</module>
4647
<module>leetcode-extends</module>

nowcoder/pom.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.devyy</groupId>
8+
<artifactId>leetcode-hub-java</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>nowcoder</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>8</maven.compiler.source>
16+
<maven.compiler.target>8</maven.compiler.target>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>com.devyy</groupId>
23+
<artifactId>algo-core</artifactId>
24+
<version>1.0-SNAPSHOT</version>
25+
</dependency>
26+
</dependencies>
27+
</project>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package nc60245;
2+
3+
import java.util.Scanner;
4+
5+
public class NC60245A {
6+
public static void main(String[] args) {
7+
Scanner scanner = new Scanner(System.in);
8+
int n = scanner.nextInt();
9+
System.out.println(solve(n));
10+
}
11+
12+
private static String solve(int n) {
13+
String[] ans = new String[n * 4];
14+
for (int i = 0; i < n * 3; i++) {
15+
// String s = "*".repeat(n) + ".".repeat(n * 2) + "*".repeat(n);
16+
String s = repeat("*", n) + repeat(".", n * 2) + repeat("*", n);
17+
ans[i] = s;
18+
}
19+
int j = 1;
20+
for (int i = n * 3; i < n * 4; i++) {
21+
// String s = ".".repeat(j) + "*".repeat(n) + ".".repeat(n - j)
22+
// + ".".repeat(n - j) + "*".repeat(n) + ".".repeat(j);
23+
String s = repeat(".", j) + repeat("*", n) + repeat(".", n - j)
24+
+ repeat(".", n - j) + repeat("*", n) + repeat(".", j);
25+
ans[i] = s;
26+
j++;
27+
}
28+
return String.join(System.lineSeparator(), ans);
29+
}
30+
31+
static String repeat(String s, int count) {
32+
StringBuilder stringBuilder = new StringBuilder();
33+
for (int i = 0; i < count; i++) {
34+
stringBuilder.append(s);
35+
}
36+
return stringBuilder.toString();
37+
}
38+
}
39+
/*
40+
牛客周赛 Round 1
41+
游游画U
42+
https://ac.nowcoder.com/acm/contest/60245/A
43+
44+
题目描述
45+
游游想让你画一个大小为 n 的 "U" ,你能帮帮她吗?
46+
具体的画法见样例说明。
47+
输入描述:
48+
一个正整数 n。
49+
1≤n≤50
50+
输出描述:
51+
共输出4n 行,每行输出一个长度为4n 的,仅包含 '*' 和 '.' 两种字符的字符串。
52+
53+
找规律
54+
======
55+
56+
示例1
57+
输入
58+
1
59+
输出
60+
*..*
61+
*..*
62+
*..*
63+
.**.
64+
65+
示例2
66+
输入
67+
2
68+
输出
69+
**....**
70+
**....**
71+
**....**
72+
**....**
73+
**....**
74+
**....**
75+
.**..**.
76+
..****..
77+
78+
示例3
79+
输入
80+
3
81+
输出
82+
***......***
83+
***......***
84+
***......***
85+
***......***
86+
***......***
87+
***......***
88+
***......***
89+
***......***
90+
***......***
91+
.***....***.
92+
..***..***..
93+
...******...
94+
*/
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package nc60245;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.Scanner;
6+
7+
public class NC60245B {
8+
static int n;
9+
static int[] a;
10+
static String s;
11+
12+
public static void main(String[] args) {
13+
Scanner scanner = new Scanner(System.in);
14+
n = scanner.nextInt();
15+
a = new int[n];
16+
for (int i = 0; i < n; i++) {
17+
a[i] = scanner.nextInt();
18+
}
19+
s = scanner.next();
20+
System.out.println(solve());
21+
}
22+
23+
private static String solve() {
24+
Map<Integer, Integer> red = new HashMap<>();
25+
Map<Integer, Integer> blue = new HashMap<>();
26+
for (int i = 0; i < n; i++) {
27+
if (s.charAt(i) == 'R') {
28+
red.put(a[i], red.getOrDefault(a[i], 0) + 1);
29+
} else {
30+
blue.put(a[i], blue.getOrDefault(a[i], 0) + 1);
31+
}
32+
}
33+
34+
long ans = 0;
35+
for (Map.Entry<Integer, Integer> entry : red.entrySet()) {
36+
long rcnt = entry.getValue();
37+
int bcnt = blue.getOrDefault(entry.getKey(), 0);
38+
ans += rcnt * bcnt;
39+
}
40+
return String.valueOf(ans);
41+
}
42+
}
43+
/*
44+
游游的数组染色
45+
https://ac.nowcoder.com/acm/contest/60245/B
46+
47+
题目描述
48+
游游拿到了一个数组,其中一些数被染成红色,一些数被染成蓝色。
49+
游游想知道,取两个不同颜色的数,且它们的数值相等,有多少种不同的取法?
50+
我们定义,两种取法如果取的某个数在原数组的位置不同,则定义为不同的取法。
51+
输入描述:
52+
第一行输入一个正整数n,代表数组的长度。
53+
第二行输入n 个正整数ai,代表数组中的元素。
54+
第三行输入一个长度为n ,仅包含 'R' 和 'B' 两种字符的字符串,第i 个字符为 'R' 代表数组第i 个元素被染成红色,为 'B' 代表被染成蓝色。
55+
1≤n≤200000
56+
1≤ai≤10^9
57+
输出描述:
58+
输出一个整数,代表取数的方案数量。
59+
60+
乘法原理
61+
======
62+
63+
示例1
64+
输入
65+
5
66+
1 2 1 2 2
67+
BRRBB
68+
输出
69+
3
70+
说明
71+
第一种取法,取第一个数和第三个数,颜色不同且都是1。
72+
第二种取法,取第二个数和第四个数,颜色不同且都是2。
73+
第三种取法,取第二个数和第五个数,颜色不同且都是2。
74+
75+
示例2
76+
输入
77+
3
78+
2 3 3
79+
BBB
80+
输出
81+
0
82+
说明
83+
所有数都是蓝色,显然取不出两个颜色不同的数。
84+
*/

0 commit comments

Comments
 (0)