Skip to content

Commit dbd0694

Browse files
committed
feat: commit solution 66
1 parent ff67f50 commit dbd0694

File tree

6 files changed

+111
-2
lines changed

6 files changed

+111
-2
lines changed

_sidebar.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
- [27.移除元素 ✅](solution/1-99/0027.remove-element/)
1919
- [28.实现 strStr() ✅](solution/1-99/0028.implement-strstr/)
2020
- [35.搜索插入位置 ✅](solution/1-99/0035.search-insert-position/)
21+
- [66.加一 ✅](solution/1-99/0066.plus-one/)
2122
- [88.合并两个有序数组](solution/1-99/0088.merge-sorted-array/)
2223
- [125.验证回文串 ✅](solution/100-199/0125.valid-palindrome/)
2324

2425

2526

26-
2727

2828

index-tags.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
| [27](https://leetcode-cn.com/problems/remove-element) | [移除元素](/solution/1-99/0027.remove-element/) | `数组`,`双指针` | <font color=green>简单</font> ||
2929
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> ||
3030
| [88](https://leetcode-cn.com/problems/merge-sorted-array) | [合并两个有序数组](/solution/1-99/0088.merge-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
31+
| [66](https://leetcode-cn.com/problems/plus-one) | [加一](/solution/1-99/0066.plus-one/) | `数组` | <font color=green>简单</font> ||
3132

3233
#### **哈希表**
3334

index-type.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
| [38](https://leetcode-cn.com/problems/count-and-say) | [外观数列](/solution/1-99/0038.count-and-say/) | `字符串` | <font color=green>简单</font> |
2222
| [53](https://leetcode-cn.com/problems/maximum-subarray) | [最大子序和](/solution/1-99/0053.maximum-subarray/) | `数组`,`分治算法`,`动态规划` | <font color=green>简单</font> |
2323
| [58](https://leetcode-cn.com/problems/length-of-last-word) | [最后一个单词的长度](/solution/1-99/0058.length-of-last-word/) | `字符串` | <font color=green>简单</font> |
24-
| [66](https://leetcode-cn.com/problems/plus-one) | [加一](/solution/1-99/0066.plus-one/) | `数组` | <font color=green>简单</font> |
24+
| [66](https://leetcode-cn.com/problems/plus-one) | [加一](/solution/1-99/0066.plus-one/) | `数组` | <font color=green>简单</font> ||
2525
| [67](https://leetcode-cn.com/problems/add-binary) | [二进制求和](/solution/1-99/0067.add-binary/) | `数学`,`字符串` | <font color=green>简单</font> |
2626
| [69](https://leetcode-cn.com/problems/sqrtx) | [x 的平方根](/solution/1-99/0069.sqrt%28x%29/) | `数学`,`二分查找` | <font color=green>简单</font> |
2727
| [70](https://leetcode-cn.com/problems/climbing-stairs) | [爬楼梯](/solution/1-99/0070.climbing-stairs/) | `动态规划` | <font color=green>简单</font> |

solution/1-99/0066.plus-one/README.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# [66.加一](https://leetcode-cn.com/problems/plus-one/)
2+
3+
4+
### 题目描述
5+
6+
<div class="notranslate"><p>给定一个由<strong>整数</strong>组成的<strong>非空</strong>数组所表示的非负整数,在该数的基础上加一。</p>
7+
8+
<p>最高位数字存放在数组的首位, 数组中每个元素只存储<strong>单个</strong>数字。</p>
9+
10+
<p>你可以假设除了整数 0 之外,这个整数不会以零开头。</p>
11+
12+
<p><strong>示例&nbsp;1:</strong></p>
13+
14+
<pre><strong>输入:</strong> [1,2,3]
15+
<strong>输出:</strong> [1,2,4]
16+
<strong>解释:</strong> 输入数组表示数字 123。
17+
</pre>
18+
19+
<p><strong>示例&nbsp;2:</strong></p>
20+
21+
<pre><strong>输入:</strong> [4,3,2,1]
22+
<strong>输出:</strong> [4,3,2,2]
23+
<strong>解释:</strong> 输入数组表示数字 4321。
24+
</pre>
25+
</div>
26+
27+
### 解题思路
28+
29+
1. 末位无进位,则末位加一即可,因为末位无进位,前面也不可能产生进位,比如 123 => 124
30+
2. 末位有进位,在中间位置进位停止,则需要找到进位的典型标志,即为当前位 %10后为 0,则前一位加 1,直到不为 0 为止,比如 499 => 500
31+
3. 末位有进位,并且一直进位到最前方导致结果多出一位,对于这种情况,需要在第 2 种情况遍历结束的基础上,进行单独处理,比如 999 => 1000
32+
33+
### 代码实现
34+
35+
<!-- tabs:start -->
36+
37+
#### **Golang**
38+
```go
39+
func plusOne(digits []int) []int {
40+
n := len(digits)
41+
for i := n - 1; i >= 0; i-- {
42+
if digits[i] < 9 {
43+
digits[i] += 1
44+
return digits
45+
} else {
46+
digits[i] = 0
47+
}
48+
}
49+
ret := make([]int, n+1)
50+
ret[0] = 1
51+
return ret
52+
}
53+
```
54+
55+
56+
<!-- tabs:end -->
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=66 lang=golang
5+
*
6+
* [66] 加一
7+
*/
8+
9+
// @lc code=start
10+
func plusOne(digits []int) []int {
11+
n := len(digits)
12+
for i := n - 1; i >= 0; i-- {
13+
if digits[i] < 9 {
14+
digits[i] += 1
15+
return digits
16+
} else {
17+
digits[i] = 0
18+
}
19+
}
20+
ret := make([]int, n+1)
21+
ret[0] = 1
22+
return ret
23+
}
24+
25+
// @lc code=end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestPlusOne(t *testing.T) {
8+
var nums []int
9+
var ret []int
10+
11+
nums = []int{1, 2, 3}
12+
ret = []int{1, 2, 4}
13+
plus := plusOne(nums)
14+
for k, v := range ret {
15+
if plus[k] != v {
16+
t.Fatalf("case fails: %v\n", plus)
17+
}
18+
}
19+
nums = []int{9, 9, 9}
20+
ret = []int{1, 0, 0, 0}
21+
plus = plusOne(nums)
22+
for k, v := range ret {
23+
if plus[k] != v {
24+
t.Fatalf("case fails: %v\n", plus)
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)