23_leetcode_plusOne
来源:互联网 发布:linux pipe size 编辑:程序博客网 时间:2024/05/03 21:50
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.
1:当数组为空时;2:注意进位;3数组中数字从前到后交换
vector<int> plusOne(vector<int> &digits) { vector<int> result; if(digits.size() == 0) { result.push_back(1); return result; } //最低位在末尾 int size = (int)digits.size(); int count = 1; for(int i = size - 1; i >= 0; i--) { int temp = digits[i] + count; if(temp >= 10) { result.push_back(temp - 10); count = 1; } else { result.push_back(temp); count = 0; } } if(count == 1) { result.push_back(1); } size = (int)result.size(); int start = 0; int end = size - 1; while(start < end) { swap(result[start++], result[end--]); } return result; }
0 0