leetcode: Plus One
2015-02-09 14:27
381 查看
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.
The digits are stored such that the most significant digit is at the head of the list.
class Solution { public: vector<int> plusOne(vector<int> &digits) { vector<int> retVtr; if (digits.size() == 0) return retVtr; int len = digits.size(); int carry = 0; for (int i=len-1; i>=0; i--) { int newVal; if (i == len-1) newVal = digits[len-1]+1; else newVal = digits[i]+carry; if (newVal >= 10) { carry = 1; newVal = newVal%10; } else { carry = 0; } retVtr.push_back(newVal); } if (carry) { retVtr.push_back(1); } vector<int> result; for (int i=retVtr.size()-1; i>=0; i--) result.push_back(retVtr[i]); return result; } };
相关文章推荐
- [LeetCode][JavaScript]Plus One
- Leetcode 66 Plus One
- LeetCode | Plus One
- leetcode--66. Plus One
- LeetCode66——Plus One(一个整数用数组存储,然后在末尾加1)
- [LeetCode] Plus One
- LeetCode: Plus One
- leetcode 65:Plus One
- [LeetCode] Plus One 加一运算
- Plus One -- LeetCode
- Leetcode65 Plus One
- [LeetCode] Plus One
- leetcode-66 Plus One
- LeetCode 第 66 题 (Plus One)
- 字符串可以按位获得和列表生成式-【leetcode66-plus one】
- Java for LeetCode 066 Plus One
- [LeetCode]66 Plus One
- [LeetCode] Plus One
- [LeetCode]Plus One
- LeetCode-Plus One