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

Commit5ab7cd5

Browse files
authored
added solution and test case for 128 (fishercoder1534#151)
1 parenta07318b commit5ab7cd5

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

‎src/main/java/com/fishercoder/solutions/_128.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,28 @@ public int longestConsecutive(int[] nums) {
113113
returnmax;
114114
}
115115
}
116+
117+
publicstaticclassSolution3 {
118+
publicintlongestConsecutive(int[]nums) {
119+
HashSet<Integer>numSet =newHashSet<>();
120+
for (intnum :nums) {
121+
numSet.add(num);
122+
}
123+
124+
intlongestStreak =0;
125+
for (intnum :nums) {
126+
if (!numSet.contains(num -1)) {
127+
intcurrentNum =num;
128+
intcurrentStreak =1;
129+
130+
while (numSet.contains(currentNum +1)) {
131+
currentNum +=1;
132+
currentStreak +=1;
133+
}
134+
longestStreak =Math.max(longestStreak,currentStreak);
135+
}
136+
}
137+
returnlongestStreak;
138+
}
139+
}
116140
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._128;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_128Test {
10+
privatestatic_128.Solution3solution3;
11+
privatestaticint[]nums;
12+
13+
@BeforeClass
14+
publicstaticvoidsetup() {
15+
solution3 =new_128.Solution3();
16+
}
17+
18+
@Test
19+
publicvoidtest1() {
20+
nums =newint[]{100,4,200,1,3,2};
21+
intresult =4;
22+
assertEquals(4,solution3.longestConsecutive(nums));
23+
}
24+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp