Created
November 7, 2018 00:44
-
-
Save qiaoxu123/d7249a1a59ff6f4ef459f083cf3ab231 to your computer and use it in GitHub Desktop.
给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。 你可以假设除了整数 0 之外,这个整数不会以零开头。 示例 1: 输入: [1,2,3]
输出: [1,2,4]
解释: 输入数组表示数字 123。
示例 2: 输入: [4,3,2,1]
输出: [4,3,2,2]
解释: 输入数组表示数字 4321。
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
class Solution { | |
public: | |
vector<int> plusOne(vector<int>& digits) { | |
int len = digits.size(); | |
vector<int> array = digits; | |
array[len - 1] += 1; | |
for(int i = len - 1;i > 0;i--){ | |
if(array[i] >= 10){ | |
array[i] = array[i] % 10; | |
array[i - 1] += 1; | |
} | |
} | |
if(array[0] >= 10){ | |
array[0] = array[0] % 10; | |
array.insert(array.begin(),1); | |
} | |
return array; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment