-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
|
||
func plusOne(digits []int) []int { | ||
lastI := len(digits) - 1 | ||
digits[lastI] += 1 | ||
currentI := lastI | ||
for currentI >= 0 && digits[currentI] > 9 { | ||
digits[currentI] = 0 | ||
currentI-- | ||
if currentI >= 0 { | ||
digits[currentI]++ | ||
} | ||
} | ||
if digits[0] == 0 { | ||
digits = append([]int{1}, digits...) | ||
} | ||
return digits | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
// Example 1: | ||
|
||
// Input: digits = [1,2,3] | ||
// Output: [1,2,4] | ||
// Explanation: The array represents the integer 123. | ||
// Incrementing by one gives 123 + 1 = 124. | ||
// Thus, the result should be [1,2,4]. | ||
// Example 2: | ||
|
||
// Input: digits = [4,3,2,1] | ||
// Output: [4,3,2,2] | ||
// Explanation: The array represents the integer 4321. | ||
// Incrementing by one gives 4321 + 1 = 4322. | ||
// Thus, the result should be [4,3,2,2]. | ||
// Example 3: | ||
|
||
// Input: digits = [9] | ||
// Output: [1,0] | ||
// Explanation: The array represents the integer 9. | ||
// Incrementing by one gives 9 + 1 = 10. | ||
// Thus, the result should be [1,0]. | ||
|
||
func TestPlusOne(t *testing.T) { | ||
t.Run("1, 2, 3", func(t *testing.T) { | ||
|
||
digits := []int{1, 2, 3} | ||
want := []int{1, 2, 4} | ||
|
||
got := plusOne(digits) | ||
|
||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("got %v, want %v", got, want) | ||
} | ||
}) | ||
t.Run("4,3,2,1", func(t *testing.T) { | ||
|
||
digits := []int{4, 3, 2, 1} | ||
want := []int{4, 3, 2, 2} | ||
|
||
got := plusOne(digits) | ||
|
||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("got %v, want %v", got, want) | ||
} | ||
}) | ||
t.Run("4,3,2,1", func(t *testing.T) { | ||
|
||
digits := []int{9} | ||
want := []int{1, 0} | ||
|
||
got := plusOne(digits) | ||
|
||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("got %v, want %v", got, want) | ||
} | ||
}) | ||
} |