LeetCode 66. Plus One

来源:互联网 发布:哪里有卖淘宝买家信息 编辑:程序博客网 时间:2024/06/14 03:41

Plus One


题目描述:

Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.

You may assume the integer do not contain any leading zero, except the number 0 itself.

The digits are stored such that the most significant digit is at the head of the list.



题目大意:

给定一个数组digits,第一个元素不为0,如digits[1,2,3,4,5,6],用这个数组模拟数字123456,现在需要你做的操作你将123456加1,变成123457,数组变成digits[1,2,3,4,5,7]。


题目代码:

class Solution {public:    vector<int> plusOne(vector<int>& digits) {        for(int i = digits.size()-1; i >= 0; i--){            if(digits[i] != 9){                digits[i]+=1;                return digits;            }else{                digits[i] = 0;            }        }        digits.insert(digits.begin(),1);        return digits;    }};




原创粉丝点击