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

[pull] main from itcharge:main #5

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
pull merged 5 commits into AlgorithmAndLeetCode:main from itcharge:main
Jun 18, 2022
Merged
Changes from 1 commit
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
Next Next commit
Update 0066. 加一.md
  • Loading branch information
杨世超 committed Jun 17, 2022
commit 8ffcae401cd99bdd5469a2ead4758857922d5af3
33 changes: 25 additions & 8 deletions Solutions/0066. 加一.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,40 @@

## 题目大意

给定一个非负整数数组,数组每一位对应整数的一位数字。计算整数 +1 后的结果。
**描述**:给定一个非负整数数组,数组每一位对应整数的一位数字。

**要求**:计算整数加 `1` 后的结果。

**说明**:

- 1ドル \le digits.length \le 100$。
- 0ドル \le digits[i] \le 9$。

**示例**:

```Python
输入 digits = [1,2,3]
输出 [1,2,4]
解释 输入数组表示数字 123,加 1 之后为 124。
```

## 解题思路

这道题把整个数组看成了一个整数,然后个位数 +1。问题的实质是利用数组模拟加法运算。
### 思路 1:模拟

这道题把整个数组看成了一个整数,然后个位数加 `1`。问题的实质是利用数组模拟加法运算。

如果个位数不为 9 的话,直接把个位数 +1 就好。如果个位数为 9 的话,还要考虑进位。
如果个位数不为 `9` 的话,直接把个位数加 `1` 就好。如果个位数为 `9` 的话,还要考虑进位。

具体步骤:

1. 数组前补 0 位。
2. 将个位数字进行 +1 计算。
1. 数组前补 `0` 位。
2. 将个位数字进行加 `1` 计算。
3. 遍历数组
1. 如果该位数字 大于等于 10,则向下一位进 1,继续下一位判断进位。
2. 如果该位数字 小于 10,则跳出循环。
1. 如果该位数字 大于等于 `10`,则向下一位进 `1`,继续下一位判断进位。
2. 如果该位数字小于 `10`,则跳出循环。

## 代码
### 思路 1:模拟代码

```Python
def plusOne(self, digits: List[int]) -> List[int]:
Expand Down

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