LeetCode Plus One
2014-07-21 15:40
288 查看
题目
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
代码:
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
代码:
class Solution { public: vector<int> plusOne(vector<int> &digits) { vector<int> ans; int add,carry=1; //一位的和,进位 int len=digits.size(); for(int i=len-1;i>=0;i--) { add=carry; add+=digits[i]; carry=add/10; ans.push_back(add%10); if(carry==0) //没有进位时直接复制 { while(--i>=0) ans.push_back(digits[i]); } } if(carry>=1) ans.push_back(1); reverse(ans.begin(),ans.end()); return ans; } };
相关文章推荐
- Plus One Linked List -- LeetCode
- LeetCode Plus One
- leetcode — plus-one
- Leetcode-plus-one
- Plus One leetcode java
- [LeetCode] Plus One Linked List 链表加一运算
- leetcode之Plus One
- LeetCode Plus One
- [leetCode] 369. Plus One Linked List
- 【LeetCode】Plus One && 【九度】题目1198:a+b
- LeetCode Plus One
- LeetCode之PlusONE
- 【LeetCode】66 & 67- Plus One & Add Binary
- leetcode 66 Plus One C++
- leetcode之Plus One
- LeetCode之Plus One
- leetcode Plus ONE
- LeetCode 369. Plus One Linked List
- leetcode Plus One
- 66. Plus One LeetCode