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

66. Plus One #38

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
zwfang merged 1 commit into master from plus-one
Jan 2, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ continually updating 😃.
* [7. Reverse Integer](src/0007_reverse_integer/reverse_integer.go)
* [9. Palindrome Number](src/0009_palindrome_number/palindrome_number.go)
* [13. Roman to Integer](src/0013_roman_to_integer/roman_to_integer.go)   *`string`*
* [66. Plus One](src/0066_plus_one/plus_one.go)   *`array`*

<details>
</details>
37 changes: 37 additions & 0 deletions src/0066_plus_one/plus_one.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
66. Plus One
https://leetcode.com/problems/plus-one/

Given a non-empty array of digits representing a non-negative integer,
plus one to the integer.

The digits are stored such that the most significant digit is at the head of the list,
and each element in the array contain a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.
*/
// time: 2019年01月02日

package plusone

// time complexity: O(n)
// space complexity: O(1)
func plusOne(digits []int) []int {
var (
carry = 1
index = len(digits) - 1
)
for index >= 0 {
sum := carry + digits[index]
digits[index] = sum % 10
carry = sum / 10
index--
}

if carry > 0 {
digits = append(digits, 0)
copy(digits[1:], digits[0:])
digits[0] = carry
}
return digits
}
14 changes: 14 additions & 0 deletions src/0066_plus_one/plus_one_test.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package plusone

import (
"reflect"
"testing"
)

func TestPlusOne(t *testing.T) {
digits := []int{9, 9, 9, 9}
expected := []int{1, 0, 0, 0, 0}
if res := plusOne(digits); !reflect.DeepEqual(expected, res) {
t.Errorf("expected %v, got %v", expected, res)
}
}
1 change: 1 addition & 0 deletions src/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
|0062|[Unique Paths](./0062_unique_paths/unique_paths.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*|
|0063|[Unique Paths 2](./0063_unique_paths_2/unique_paths2.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*|
|0064|[Minimum Path Sum](./0064_minimum_path_sum/minimum_path_sum.go)|Medium|*`dynamic programming;`* *` dfs`*|
|0066|[66. Plus One](0066_plus_one/plus_one.go)|Easy|*`math`*|
|0067|[add Binary](./0067_add_binary/add_binary.go)|Easy||
|0069|[Sqrt(x)](0069_sqrtx/sqrtx.go)|Easy|*`binary search`*|
|0070|[Climbing Stairs](./0070_climbing_stairs/climbing_stairs.go)|Easy|*`dynamic programming`*|
Expand Down

AltStyle によって変換されたページ (->オリジナル) /