-
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
1 changed file
with
42 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,42 @@ | ||
# 66. 加一 | ||
|
||
给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一。 | ||
|
||
最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。 | ||
|
||
你可以假设除了整数 0 之外,这个整数不会以零开头。 | ||
|
||
来源:力扣(LeetCode) | ||
|
||
链接:<https://leetcode-cn.com/problems/plus-one> | ||
|
||
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 | ||
|
||
示例 1: | ||
|
||
```js | ||
输入:digits = [1,2,3] | ||
输出:[1,2,4] | ||
解释:输入数组表示数字 123。 | ||
``` | ||
|
||
--- | ||
|
||
```js | ||
/** | ||
* @param {number[]} digits | ||
* @return {number[]} | ||
*/ | ||
var plusOne = function(digits) { | ||
for (let i = digits.length-1; i >=0; i --) { | ||
if (digits[i] === 9) { | ||
digits[i] = 0 | ||
} else { | ||
digits[i] = digits[i] + 1 | ||
break | ||
} | ||
} | ||
if (digits[0] === 0) return [1, ...digits] | ||
else return digits | ||
}; | ||
``` |