leetcode - plus one

来源:互联网 发布:淘宝装修备份在哪 编辑:程序博客网 时间:2024/05/16 05:55
/*Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.*/#include <iostream>#include <vector>using std::cout;using std::endl;using std::vector;std::vector<int>::iterator;std::vector<int>::reverse_iterator;class Solution {public:vector<int> plusOne(vector<int> &digits) {vector<int>::reverse_iterator p;for (p = digits.rbegin(); p != digits.rend(); p++) {if (*p + 1 < 10){*p = *p + 1;break;}else {*p = 0;}}if (p == digits.rend()) {digits.insert(digits.begin(), 1);}return digits;}};void main(){vector<int> digits;/*digits.push_back(1);digits.push_back(3);digits.push_back(4);*/digits.push_back(9);digits.push_back(9);Solution slt;slt.plusOne(digits);}

0 0
原创粉丝点击