Movatterモバイル変換


[0]ホーム

URL:


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

Commitfa75bc2

Browse files
[LEET-3354] add 3354
1 parenta87e53d commitfa75bc2

File tree

3 files changed

+78
-0
lines changed
  • paginated_contents/algorithms/4th_thousand
  • src

3 files changed

+78
-0
lines changed

‎paginated_contents/algorithms/4th_thousand/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
3+
| 3354|[Make Array Elements Equal to Zero](https://leetcode.com/problems/make-array-elements-equal-to-zero/)|[Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3354.java)|| Easy|
34
| 3349|[Adjacent Increasing Subarrays Detection I](https://leetcode.com/problems/adjacent-increasing-subarrays-detection-i/)|[Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3349.java)|| Easy|
45
| 3340|[Check Balanced String](https://leetcode.com/problems/check-balanced-string/)|[Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3340.java)|| Easy|
56
| 3330|[Find the Original Typed String I](https://leetcode.com/problems/find-the-original-typed-string-i/)|[Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3330.java)|| Easy|
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
packagecom.fishercoder.solutions.fourththousand;
2+
3+
importjava.util.Arrays;
4+
5+
publicclass_3354 {
6+
publicstaticclassSolution1 {
7+
publicintcountValidSelections(int[]nums) {
8+
intcount =0;
9+
for (inti =0;i <nums.length;i++) {
10+
if (nums[i] ==0) {
11+
if (isValidWithMoveDirection(nums,i,true)) {
12+
count++;
13+
}
14+
if (isValidWithMoveDirection(nums,i,false)) {
15+
count++;
16+
}
17+
}
18+
}
19+
returncount;
20+
}
21+
22+
privatebooleanisValidWithMoveDirection(int[]nums,intindex,booleanmoveLeft) {
23+
int[]copy =Arrays.copyOf(nums,nums.length);
24+
while (index >=0 &&index <nums.length) {
25+
if (moveLeft) {
26+
if (copy[index] >0) {
27+
copy[index]--;
28+
moveLeft = !moveLeft;
29+
index++;
30+
}else {
31+
index--;
32+
}
33+
}else {
34+
if (copy[index] >0) {
35+
copy[index]--;
36+
moveLeft = !moveLeft;
37+
index--;
38+
}else {
39+
index++;
40+
}
41+
}
42+
}
43+
for (intnum :copy) {
44+
if (num !=0) {
45+
returnfalse;
46+
}
47+
}
48+
returntrue;
49+
}
50+
}
51+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
packagecom.fishercoder.fourththousand;
2+
3+
importcom.fishercoder.solutions.fourththousand._3354;
4+
importorg.junit.jupiter.api.BeforeEach;
5+
importorg.junit.jupiter.api.Test;
6+
7+
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
8+
9+
publicclass_3354Test {
10+
private_3354.Solution1solution1;
11+
12+
@BeforeEach
13+
publicvoidsetup() {
14+
solution1 =new_3354.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(2,solution1.countValidSelections(newint[]{1,0,2,0,3}));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals(3,solution1.countValidSelections(newint[]{16,13,10,0,0,0,10,6,7,8,7}));
25+
}
26+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp