[leetcode] 66.Plus One
2015-05-12 09:02
477 查看
题目:
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.
题意:
将一个非负的整数按位放在一个数组里,并且最高位在A[0],最低位在A[n-1],比如56,A[0] = 5,A[1] =6;
然后将该数加上一。
思路:
这道题主要需要计算进位,如果最高位产生了进位,那么需要重新开辟一个n+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.
题意:
将一个非负的整数按位放在一个数组里,并且最高位在A[0],最低位在A[n-1],比如56,A[0] = 5,A[1] =6;
然后将该数加上一。
思路:
这道题主要需要计算进位,如果最高位产生了进位,那么需要重新开辟一个n+1的数组来存放。
以上。
代码如下:
class Solution { public: vector<int> plusOne(vector<int>& digits) { int size = digits.size(); if(size == 0)return vector<int>(1,1); int carry = 1; for(int i = size - 1; i >= 0; --i){ digits[i] += carry; carry = digits[i]/10; digits[i] %= 10; } if(carry == 1){ vector<int> result(1,1); for(int i = 0; i < size; ++i) result.push_back(digits[i]); return result; } return digits; } };
相关文章推荐
- 个人记录-LeetCode 66. Plus One
- 2018.3.12 leetcode打卡 #66 plus one
- 【leetcode】【66】Plus One
- Leetcode 66 Plus One
- 【leetcode】66. Plus One
- LeetCode 66 Plus One
- leetcode-66-Plus One
- LeetCode 66. Plus One
- [LeetCode 66] Plus One
- leetcode 每日一题 66. Plus One
- LeetCode WIith JS || 66. Plus One [数组存储的数据加一]
- [Leetcode从零开刷]66. Plus One
- Leetcode 66. Plus One
- leetcode66: Plus One
- 【leetcode】66. Plus One
- LeetCode: 66. Plus One
- leetcode-66 plus one
- 【一天一道LeetCode】#66. Plus One
- 2018.3.12 leetcode打卡 #66 plus one
- [leetcode 66] Plus One