Skip to content

Commit 40e9b42

Browse files
committed
add 66
1 parent 435bd73 commit 40e9b42

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

Readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
| 23 | [合并 K 个排序链表](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第23号问题:合并K个排序链表.md) |
2727
| 24 | [两两交换链表中的节点](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第24号问题:两两交换链表中的节点.md) |
2828
| 26 | [删除排序数组中的重复项](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第26号问题:删除排序数组中的重复项.md) |
29-
| 75 | [颜色分类](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第75号问题:颜色分类.md) |
29+
| 66 | [加一](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第66号问题:加一.md) |
30+
| 75 | [µ颜色分类](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第75号问题:颜色分类.md) |
3031
| 86 | [分割链表](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第86号问题:分割链表.md) |
3132
| 92 | [反转链表 II](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第92号问题:反转链表II.md) |
3233
| 94 | [二叉树的中序遍历](https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第94号问题:二叉树的中序遍历.md) |

notes/LeetCode第2号问题:两数相加.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,5 @@ public:
7777

7878

7979

80-
![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/blog/fz0rq.png)
80+
81+
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# LeetCode 第 66 号问题:加一
2+
3+
> 本文首发于公众号「五分钟学算法」,是[图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>)系列文章之一。
4+
5+
> 个人网站:[https://www.cxyxiaowu.com](
6+
7+
今天分享的题目来源于 LeetCode 上第 66 号问题:加一。题目难度为 Easy,目前通过率为 39.0% 。
8+
9+
### 题目描述
10+
11+
给定一个由**整数**组成的**非空**数组所表示的非负整数,在该数的基础上加一。
12+
13+
最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。
14+
15+
你可以假设除了整数 0 之外,这个整数不会以零开头。
16+
17+
**示例 1:**
18+
19+
```
20+
输入: [1,2,3]
21+
输出: [1,2,4]
22+
解释: 输入数组表示数字 123。
23+
```
24+
25+
**示例 2:**
26+
27+
```
28+
输入: [4,3,2,1]
29+
输出: [4,3,2,2]
30+
解释: 输入数组表示数字 4321。
31+
```
32+
33+
**示例 3:**
34+
35+
```
36+
//为了更好理解题意,根据 LeetCode 评论区评论新增一个示例
37+
输入: [9,9]
38+
输出: [1,0,0]
39+
解释: 输入数组表示数字 100。
40+
```
41+
42+
### 题目解析
43+
44+
本题很简单,题目意思也很好理解,注意的点就是 **进位问题**
45+
46+
* 如果数组末位(个位)小于 9 ,直接个位加 1 返回即可
47+
48+
* 如果数组末位(个位)等于 9,将该位(个位)设置为 0 ,并且产生了进位,接下来观察前一位(十位)
49+
50+
* * 如果前一位(十位)小于 9 ,直接十位加 1 返回即可
51+
* 如果前一位(十位)等于 9,将该位(十位)设置为 0 ,并且产生了进位,接下来观察前一位(百位)
52+
53+
* 以此类推,最后观察运算完的第一位是否为 0 ,如果为 0 ,则在最前面加 1 (**示例 3**
54+
55+
56+
57+
### 动画描述
58+
59+
![](https://raw.githubusercontent.com/MisterBooo/myBlogPic/master/20190606112155.gif)
60+
61+
![](https://raw.githubusercontent.com/MisterBooo/myBlogPic/master/20190606112128.gif)
62+
63+
64+
65+
### 代码实现
66+
67+
```java
68+
public class Solution {
69+
public int[] plusOne(int[] digits) {
70+
int n = digits.length;
71+
//从数组末尾开始向前遍历
72+
for (int i = digits.length - 1; i >= 0; --i) {
73+
if (digits[i] < 9) {
74+
digits[i]++;
75+
//没有进位,直接返回
76+
return digits;
77+
}
78+
//产生进位,需要将该位赋值为 0
79+
digits[i] = 0;
80+
}
81+
//整体产生了进位,数组长度需要变化加 1
82+
int[] res = new int[n + 1];
83+
res[0] = 1;
84+
return res;
85+
}
86+
}
87+
```
88+
89+
![](https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/blog/fz0rq.png)
90+

0 commit comments

Comments
 (0)