第十周:66. Plus One

来源:互联网 发布:分期乐软件靠谱吗 编辑:程序博客网 时间:2024/06/16 11:22

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.

Subscribe to see which companies asked this question.

解题思路在代码里呈现:

class Solution {public:    vector<int> plusOne(vector<int>& digits) {     int flag = 1; //设置标志位        for(int i = digits.size() - 1; i >= 0; -- i)       {           int a = digits[i] + flag;            digits[i] = a % 10;//当前数组值为a模10的结果            flag = a / 10;//是否有进位        }        if(flag == 1)             digits.insert(digits.begin(), 1);//如果最后有进位,那么在数组头插入元素1.                  return digits;    }};

0 0
原创粉丝点击