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

added solution and test case for 128#151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fishercoder1534 merged 1 commit intofishercoder1534:masterfromashmichheda:128
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletionssrc/main/java/com/fishercoder/solutions/_128.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -113,4 +113,28 @@ public int longestConsecutive(int[] nums) {
return max;
}
}

public static class Solution3 {
public int longestConsecutive(int[] nums) {
HashSet<Integer> numSet = new HashSet<>();
for (int num : nums) {
numSet.add(num);
}

int longestStreak = 0;
for (int num : nums) {
if (!numSet.contains(num - 1)) {
int currentNum = num;
int currentStreak = 1;

while (numSet.contains(currentNum + 1)) {
currentNum += 1;
currentStreak += 1;
}
longestStreak = Math.max(longestStreak, currentStreak);
}
}
return longestStreak;
}
}
}
24 changes: 24 additions & 0 deletionssrc/test/java/com/fishercoder/_128Test.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
package com.fishercoder;

import com.fishercoder.solutions._128;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class _128Test {
private static _128.Solution3 solution3;
private static int[] nums;

@BeforeClass
public static void setup() {
solution3 = new _128.Solution3();
}

@Test
public void test1() {
nums = new int[]{100,4,200,1,3,2};
int result = 4;
assertEquals(4, solution3.longestConsecutive(nums));
}
}

[8]ページ先頭

©2009-2025 Movatter.jp