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

add _66.java solution2#116

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 3 commits intofishercoder1534:masterfromWillShen7789:master
Oct 25, 2020
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
17 changes: 17 additions & 0 deletionssrc/main/java/com/fishercoder/solutions/_66.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,4 +20,21 @@ public int[] plusOne(int[] digits) {
return newNumber;
}
}

public static class Solution2 {
public int[] plusOne(int[] digits) {
int len = digits.length;
for (int i = len - 1; i >= 0; i--) {
if (digits[i] == 9) {
digits[i] = 0;
} else {
digits[i]++;
return digits;
}
}
int[] newNumber = new int[len + 1];
newNumber[0] = 1;
return newNumber;
}
}
}
26 changes: 24 additions & 2 deletionssrc/test/java/com/fishercoder/_66Test.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
package com.fishercoder;

import com.fishercoder.solutions._66;
import static org.junit.Assert.assertArrayEquals;

import org.junit.BeforeClass;
import org.junit.Test;

importstatic org.junit.Assert.assertArrayEquals;
importcom.fishercoder.solutions._66;

public class _66Test {
private static _66.Solution1 solution1;
private static _66.Solution2 solution2;
private static int[] digits;

@BeforeClass
public static void setup() {
solution1 = new _66.Solution1();
solution2 = new _66.Solution2();
}

@Test
Expand All@@ -32,4 +35,23 @@ public void test3() {
digits = new int[]{2, 4, 9, 3, 9};
assertArrayEquals(new int[]{2, 4, 9, 4, 0}, solution1.plusOne(digits));
}

@Test
public void test4() {
digits = new int[]{9, 9, 9, 9, 9};
assertArrayEquals(new int[]{1, 0, 0, 0, 0, 0}, solution2.plusOne(digits));
}

@Test
public void test5() {
digits = new int[]{8, 9, 9, 9, 9};
assertArrayEquals(new int[]{9, 0, 0, 0, 0}, solution2.plusOne(digits));
}

@Test
public void test6() {
digits = new int[]{2, 4, 9, 4, 9};
assertArrayEquals(new int[]{2, 4, 9, 5, 0}, solution2.plusOne(digits));
}

}

[8]ページ先頭

©2009-2025 Movatter.jp