前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【LeetCode】关关的刷题日记23——Leetcode 66. Plus One

【LeetCode】关关的刷题日记23——Leetcode 66. Plus One

作者头像
WZEARW
发布2018-04-09 15:53:59
7160
发布2018-04-09 15:53:59
举报
文章被收录于专栏:专知专知

关小刷刷题 23——Leetcode 66. Plus One

题目

Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.

You may assume the integer do not contain any leading zero, except the number 0 itself.

The digits are stored such that the most significant digit is at the head of the list.

题目的意思是给定一个非空数组来表示一个非负整数,除了0这个数本身,其它的数都没有前导零,将这个数加一,求得到的结果,要求仍然用数组来表示。数组的前面存数的高位。

思路

思路:计算的过程就和加法笔算类似,注意进位,尤其是最高位有进位的时候需要额外处理一下。时间复杂度O(n).

代码语言:javascript
复制
class Solution {public:
    vector<int> plusOne(vector<int>& digits) {
        int carry=1, n=digits.size();
        vector<int>output(n,0);
        for(int i=n-1; i>=0; i--)
        {
            output[i]=(digits[i]+carry)%10;
            carry=(digits[i]+carry)/10;
        }
        if(carry==1)
            output.insert(output.begin(),1);
        return output;
    }};

自己强大最重要了,尤其是女孩子,加油!

以上就是关关关于这道题的总结经验,希望大家能够理解,有什么问题可以在我们的专知公众号平台上交流或者加我们的QQ专知-人工智能交流群 426491390,也可以加入专知——Leetcode刷题交流群(请先加微信小助手weixinhao: Rancho_Fang)。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-11-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 专知 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 关小刷刷题 23——Leetcode 66. Plus One
  • 题目
  • 思路
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档