LeetCode 66. Plus One

原创 2016年06月01日 15:39:37

问题描述:

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.

问题大意:用一个数组表示非负数,可以理解为一个用数组表示的大数,然后用这个大数加上一,返回结果。

AC代码:

 vector<int> plusOne(vector<int>& digits) 
    {
        int n = digits.size();
        int val = 1;
        for(int i = n-1;i>=0;i--)
        {
            digits[i] = digits[i] + val;
            val = digits[i] /10;//判断是否有进位
            digits[i] = digits[i] % 10;
        }
        if(val > 0)
            digits.insert(digits.begin(),1);
        return digits;
    }


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode --- 66. Plus One

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

leetcode 66. Plus One

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

leetcode-66. Plus One

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

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

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

leetcode 66. Plus One

66. Plus One(整数字符加一操作) c语言实现 Description Given a non-negative integer represented ...

<LeetCode OJ> 66. Plus One

66. Plus One My Submissions Question Total Accepted: 77253 Total Submissions: 242942 Difficulty:...

[LeetCode] S66 Plus One

Plus One Given a non-negative number represented as an array of digits, plus one to the number. ...

LeetCode66: Plus One

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

LeetCode笔记:66. Plus One

用数组表示多位数字并加一

LeetCode 66 — Plus One(C++ Java Python)

题目:http://oj.leetcode.com/problems/plus-one/ Given a non-negative number represented as an arra...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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