-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathplusOne.h
More file actions
59 lines (47 loc) · 1.25 KB
/
plusOne.h
File metadata and controls
59 lines (47 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
// plusOne.h
// array
//
// Created by junlongj on 2019/7/28.
// Copyright © 2019 junl. All rights reserved.
//
#ifndef plusOne_hpp
#define plusOne_hpp
#include <stdio.h>
#include <vector>
/*
66.给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。
最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。
你可以假设除了整数 0 之外,这个整数不会以零开头。
示例 1:
输入: [1,2,3]
输出: [1,2,4]
解释: 输入数组表示数字 123。
示例 2:
输入: [4,3,2,1]
输出: [4,3,2,2]
解释: 输入数组表示数字 4321。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/plus-one
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
std::vector<int> plusOne(std::vector<int>& digits) {
std::vector<int> r;
if (digits.empty()){
return r;
}
int i=digits.size()-1;
int carry = 1;
while (i>=0) {
int t = digits[i] + carry;
carry = t/10;
t = t % 10;
r.insert(r.begin(), t);
i--;
}
if (carry > 0) {
r.insert(r.begin(), carry);
}
return r;
}
#endif /* plusOne_hpp */