LeetCode 0066 - Plus One
# Hints
- 水题
# 题面
Difficulty | Time Complexity Limit | Extra-Memory Complexity Limit |
---|---|---|
Easy |
Given a non-empty array of digits representing a non-negative integer, increment one to the integer.
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit.
You may assume the integer does not contain any leading zero, except the number itself.
Example 1:
Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Example 2:
Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
# 题意
给定一个由个位数字组成的数组,该数组表示了一个非负整数的各个数位,要求将该整数 ,并返回新的、由个位数字组成的数组。输入保证不含前导零(除 本身以外)。
# 题解
水题,直接模拟即可。
细节问题:
- 注意处理形如 的情况。
# AC代码
class Solution {
public:
vector<int> plusOne(vector<int> & digits) {
// 初始化
int n = digits.size();
vector<int> res;
// 求解
bool added = true;
for (int i = n - 1; i >= 0; i --) {
if (added) {
int x = digits[i] + 1;
res.push_back(x % 10);
added = (x == 10);
} else {
res.push_back(digits[i]);
}
}
if (added) {
res.push_back(1);
}
// 返回
reverse(res.begin(), res.end());
return res;
}
};
- 01
- Reading Papers - Kernel Concurrency06-01
- 02
- Linux Kernel - Source Code Overview05-01
- 03
- Linux Kernel - Per-CPU Storage05-01