LeetCode:Plus One

原创 2015年11月18日 13:46:58

Plus One

Total Accepted: 73167 Total Submissions: 233149 Difficulty: Easy

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.

Subscribe to see which companies asked this question

Hide Tags
 Array Math
Hide Similar Problems
 (M) Multiply Strings (E) Add Binary















code:

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int len = digits.size();
        reverse(digits.begin(), digits.end());
        int carry=1;
        for(int i=0;i<len && carry;i++) {
            int tmp =digits[i] + carry;
            carry = tmp / 10;
            tmp %= 10;
            digits[i]=tmp;
        }
        if(carry) digits.push_back(carry);
        reverse(digits.begin(),digits.end());
        return digits;
    }
};


版权声明:本文为博主原创文章,转载请注明出处。

【LeetCode-面试算法经典-Java实现】【066-Plus One(加一)】

【066-Plus One(加一)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a non-negative number represented as a...

LeetCode Plus One

题目: Given a non-negative number represented as an array of digits, plus one to the number. T...

LeetCode --- 66. Plus One

题目链接:Plus One Given a non-negative number represented as an array of digits, plus one to the number...
  • makuiyu
  • makuiyu
  • 2015年03月19日 21:53
  • 6614

leetcode 66. Plus One

leetcode 66. Plus One 题目大意:给定一个非负整数,存储在数组中,把这个数字加1,这个数的最高位在最前面。 解题思路:如果这个数的最低位为小于9,直接加到最低位去 如果这个数的最低...

[LeetCode]Plus One 优化

题目 Given a non-negative number represented as an array of digits, plus one to the number. The digits...
  • hjh00
  • hjh00
  • 2015年11月03日 11:48
  • 490

Leetcode: Plus One

题目: Given a non-negative number represented as an array of digits, plus one to the number.The digit...

leetcode笔记:Plus One

一道高精度计算的题,从低位到高位进行计算,同时考虑进位的问题,若最高位计算结果还有进位,就需要在最高位前面添加一位。可做到时间复杂度为O(n),空间复杂度为O(1)。...

LeetCode:Plus One

题目描述: Given a number represented as an array of digits, plus one to the number. 题目解释: 输入的数组digits...

leetcode-66. Plus One

Given a non-negative number represented as an array of digits, plus one to the number. The digits...
  • qj30212
  • qj30212
  • 2016年09月02日 00:34
  • 165

[C语言][LeetCode][66]Plus One

题目Plus One Given a non-negative number represented as an array of digits, plus one to the number. ...
  • Timsley
  • Timsley
  • 2015年08月11日 08:58
  • 1073
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode:Plus One
举报原因:
原因补充:

(最多只允许输入30个字)