您的位置:首页 > 其它

[LeetCode] Plus One 解题报告

2016-01-12 11:07 387 查看
Given a number represented as an array of digits, plus one to the number.
» Solve this problem

[解题思路]
加位与进位。模拟。

[code]1:  vector<int> plusOne(vector<int> &digits) {
2:      // Start typing your C/C++ solution below
3:      // DO NOT write int main() function
4:      int cary=1, sum =0;
5:      vector<int> result(digits.size(),0);
6:      for(int i = digits.size()-1; i>=0; i--)
7:      {
8:        sum = cary+digits[i];
9:        cary = sum/10;
10:        result[i] = sum%10;
11:      }
12:      if(cary >0)
13:      {
14:        result.insert(result.begin(), cary);
15:      }
16:      return result;
17:    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: