66 Plus One

Leetcode 66. Plus One-数组表示的数值+1,返回新的数组 Given anon-emptyarray of digitsrepresenting a non-negative integer, plus one to the integer. The digits are stored such that the most significant digit is at the head of the list, and each element... 阅读详情

题目链接:https://leetcode.com/problems/plus-one/

题目:

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. 保持一个进位变量。
  2. 数组的每一位都需要考虑该位数字是不是等于 9,以及进位是否为 1。

需要注意的是,当最高位有进位时,需要开辟新的数组,以满足增加的最高位,并把最高位赋值为 1,此时其它位都为 0。

注意:
为了使算法只遍历一次数组,不需要过早开辟长度大于 digits.length 的新数组。因为只有当原始数据的所有数位都为 9 时,最高位进位才会发生。

public class Solution {
    public int[] plusOne(int[] digits) {
        if(digits == null || digits.length == 0)
            return digits;
        int len = digits.length;
        int[] res = new int[len];
        int reminder = 0;
        for(int i = len - 1; i >= 0; i --) {
            if(i == len - 1) {
                if(digits[i] == 9) {
                    reminder = 1;
                    res[i] = 0;
                } else
                    res[i] = digits[i] + 1;
                continue;
            }
            if(digits[i] < 9) {
                res[i] = digits[i] + reminder;
                reminder = 0;
            }
            else {
                if(reminder == 1) {
                    res[i] = 0;
                    reminder = 1;
                } else
                    res[i] = digits[i] + reminder;
            }
        }
        if(reminder == 1) {
            res = new int[len + 1];
            res[0] = 1;
        }
        return res;
    }
}
108 / 108 test cases passed.
Status: Accepted
Runtime: 304 ms
AltiumDesigner 高效封装库管理 -- samacsys插件实战指南 本文详细介绍了如何利用samacsys插件高效管理AltiumDesigner封装库。通过实战指南,指导用户完成插件安装、云端元件搜索与放置,并分享将海量资源整合为个人标准库的管理策略,从而大幅提升PCB设计效率与可靠性,杜绝封装错误。 阅读详情

相关推荐

CoppeliaSim学习笔记(1):建立Python脚本与CoppeliaSim的连接

CoppeliaSim学习笔记,建立python和CoppeliaSim的连接,实现联调。

qq_22487889的博客 1万+

LeetCode(66)Plus One

题目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.分析该题目要求:将一整数按位存储在vector中,对其

指尖飞舞 1647

博途WinCC上位机避坑指南:从工控机选型到趋势图优化的完整流程

本文详细解析了博途WinCC上位机开发全流程,从工控机选型到趋势图优化的关键技巧。针对工业自动化场景,提供硬件配置指南、网络优化方案和性能提升策略,帮助工程师避开常见陷阱,实现稳定高效的HMI系统部署。特别强调PC Station与传统触摸屏的选型对比及SIMATIC NET配置优化。

pp12345的博客 288

LeetCode --- 66. Plus One

题目链接:Plus One 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. 这道题的要求是给定

makuiyu的专栏 1万+

Ubuntu18.04环境搭建与疑难解析:从零复现YOLO-6D姿态估计算法

本文详细介绍了在Ubuntu18.04环境下从零开始复现YOLO-6D姿态估计算法的完整流程,包括系统配置、Conda环境搭建、PyTorch安装、依赖项管理、源代码获取、数据集准备以及训练验证过程。针对Singleshotpose算法的特殊需求,提供了详细的疑难解析和性能优化技巧,帮助开发者高效完成算法复现和应用部署。

weixin_30369041的博客 461

智能座舱高通平台Display架构 - 基础篇

随着智能座舱的市场需求变化以及技术发展,座舱需要支持的屏幕会越来越多,分辨率和尺寸也会越来越大。这已经是不可逆转的趋势。传统的座舱一块显示屏就需要一块主芯片支持,这在功能和成本上都不是很好的做法。一芯多屏就是基于传统座舱的缺陷提出的新型架构和方案,并且越来越广泛的在智能座舱中得到体现和发展。本文将主要讲述智能座舱中显示子系统的硬件相关技术。中控娱乐屏ICS(Instrument Central Screen)仪表盘显示屏IC(Instrument Cluster)

Math_Light的博客 2709

Leetcode 66. Plus One

You are given alarge integerrepresented as an integer arraydigits, where eachdigits[i]is theithdigit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any lea...

weixin_58080442的博客 257

Bochs源码分析 - 26:8259中断控制器介绍

前言 现在我们跟着《x86/x64体系探索及编程》来分析17章内容 -- 8259中断控制器。这个是涉及硬件的,很遗憾,除了大学期间使用STM32搞过一个计算器,其余就没多少硬件编程经验,我在淘宝上找了下大概是这个东东。 https://pdos.csail.mit.edu/6.828/2010/readings/hardware/8259A.pdf 上面是8259A的芯片手册,里面讲的还是挺详细的,虽然里面很多东西还是看不太懂,但是结合着《x86/...

WriteAnything_的博客 983

[leetcode-66]plus one(java)

问题描述: 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.分析:这道题的题意是说,有一个非负的数组代表

中华胡杨的专栏 1084

TRK-MPC5634M开发板实战:从硬件解析到CAN、ADC外设驱动调试

在嵌入式系统开发,尤其是汽车电子和工业控制领域,微控制器(MCU)是核心执行单元。其工作原理基于处理器内核、存储器和丰富的外设接口协同工作,通过软件编程实现对硬件的精确控制。这种技术价值在于能够为严苛环境提供可靠、实时的控制解决方案。典型的应用场景包括汽车车身控制、网关、电池管理系统以及工业自动化设备。本文聚焦于恩智浦(NXP)基于Power Architecture e200z0内核的汽车级MCU——MPC5634M,并以其官方评估板TRK-MPC5634M为平台,深入探讨其硬件设计逻辑、软件环境搭建及核

weixin_29997523的博客 297

如何合法抓取TikTok视频信息和评论:完整Python爬虫教程

尽管TikTok没有开放的公共API来直接抓取视频内容和评论,开发者仍然可以通过合法的方式使用第三方API工具来获取公开数据。无论使用什么工具,都应当严格遵守TikTok的使用条款和当地的隐私法规,确保数据抓取行为符合规定。通过本文介绍的TikTok-Api库和Python技术,你可以合法地抓取公开的视频信息和评论,并进行数据存储与分析。使用这些技术时,务必确保抓取行为不会侵犯用户隐私,并遵守平台的API使用规定。

2201_76125261的博客 1717

LeetCode 66. Plus One

LeetCode 66. Plus One Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The digits are stored such that the most significant digit is at the head of the l...

zhaohaibo的博客 278

脉冲变压器电感选型参数,结构原理,工艺与注意问题总结

脉冲变压器电感简称脉冲变压器,是一种特殊类型的变压器,主要用于处理脉冲信号。它的主要特性是能够在短时间内承受高电压和大电流。脉冲变压器主要用于开关电源、脉冲功率放大、雷达脉冲调制等领域。本文对脉冲变压器的工作原理,结构特点,工艺流程,选型参数及设计注意事项进行总结。

fydar的博客 1368

[Leetcode]66.Plus One

[leetcode] 66. Plus One题目链接:https://leetcode.com/problems/plus-one/Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most signifi

weixin_37553155的博客 210

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

LeetCode 66. Plus One

题目来源:https://leetcode.com/problems/plus-one/ 问题描述 66.Plus One Easy Given anon-emptyarray of digitsrepresenting a non-negative integer, plus one to the integer. The digits are stored such that...

da_kao_la的博客 180

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Leetcode 66. Plus One 加一

Leetcode 66. Plus One 加一 标签: Leetcode 题目地址:https://leetcode-cn.com/problems/plus-one/ 题目描述 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。 你可以假设除了整数 0 之外,这个整数不会以零开头。 示例 1: 输入: [...

CoderWangSon 266

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

[勇者闯LeetCode] 66. Plus One

[勇者闯LeetCode] 66. Plus OneDescription 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

fanguzhai的博客 235

66. Plus One。

Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The digits are stored such that the most significant digit is at the head of the list, and each element...

繁城落叶 440

leetcode-66. Plus One

leetcode-66. Plus OneGiven 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 it

myan991的博客 459

Leetcode 66.加一(Plus One)

Leetcode 66.加一 1 题目描述(Leetcode题目链接)   给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。你可以假设除了整数 0 之外,这个整数不会以零开头。 输入: [1,2,3] 输出: [1,2,4] 解释: 输入数组表示数字 123。 输入: [4,3,2,1] 输出: [4,3,2,2] 解...

qq_39378221的博客 219

【LeetCode】66. Plus One

LEETCode66 传送门 解法: 需要向面试官问的问题有,可能有负数吗?数字在digits中是如何存储的,正向还是反向?开头会出现0吗? 实现上推荐的方法比我的方法更直接一些。放在循环中的if判断+return比while中的判断更易读。 Python源码: Runtime:20 ms, faster than57.47%ofPythononline sub...

weixin_42344158的博客 233

【细节处理】LeetCode 66. Plus One

LeetCode 66. Plus One 博客转载自:http://www.cnblogs.com/grandyang/p/4079357.html Solution1: class Solution { public: vector&lt;int&gt; plusOne(vector&lt;int&gt;&amp; digits) { int i = digits...

Allenlzcoder的博客 261

66. Plus One

所以a[::-1]相当于 a[-1:-len(a)-1:-1],也就是从最后一个元素到第一个元素复制一遍,即倒序。b = a[i:j:s] # 表示:i,j与上面的一样,但s表示步进,缺省为1.b = a[i:j] # 表示复制a[i]到a[j-1],以生成新的list对象。#当j缺省时,默认为len(alist), 即a[1:]相当于a[1:10]#当i缺省时,默认为0,即 a[:3]相当于 a[0:3]#所以a[i:j:1]相当于a[i:j]#从后往前数的话,最后一个位置为-1。

weixin_44245188的博客 719

leetcode刷题记录-66. Plus One

leetcode刷题记录-66. Plus One 1.题目要求   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...

zhaizhan501的博客 215
上一篇: 75 Sort Colors
下一篇: 26 Remove Duplicates from Sorted Array
加油小松鼠
博客等级 码龄15年 7粉丝 202原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值