题目:
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。
这个非负数的存储方式,是把最高有效位数字放到列表的前面。

分析:
首先考虑的是进位,当最后一位是“9”的时候,会发生向前进位。
再考虑一种特殊情况”999”,执行加一操作后,数组的长度变了,这个时候需要重新创建一个新数组。笔者采用一个标志位needAdd,用来记录循环完成时,是否还有未进的位。

代码:

public class Solution {public int[] plusOne(int[] digits) {if(digits.length==0){return new int[]{1};}int index=digits.length-1;boolean needAdd=false;while(index>=0){digits[index]++;if(digits[index]<10){needAdd=false;break;}digits[index]-=10;index--;needAdd=true;}if(needAdd){int[] digitsNew=new int[digits.length+1];for(int i=0;i<digits.length;i++){digitsNew[i+1]=digits[i];}digitsNew[0]=1;return digitsNew;}return digits;}
}

Leet Code OJ 66. Plus One [Difficulty: Easy]相关推荐

  1. Leet Code OJ 112. Path Sum [Difficulty: Easy]

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  2. Leet Code OJ 344. Reverse String [Difficulty: Easy]

    题目: Write a function that takes a string as input and returns the string reversed. Example: Given s ...

  3. Leet Code OJ 28. Implement strStr() [Difficulty: Easy]

    题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...

  4. Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]

    题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...

  5. Leet Code OJ 20. Valid Parentheses [Difficulty: Easy]

    题目: Given a string containing just the characters , determine if the input string is valid. The brac ...

  6. Leet Code OJ 1. Two Sum [Difficulty: Easy]

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  7. Leet Code OJ 223. Rectangle Area [Difficulty: Easy]

    题目: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defin ...

  8. Leet Code OJ 189. Rotate Array [Difficulty: Easy]

    题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ar ...

  9. Leet Code OJ 27. Remove Element [Difficulty: Easy]

    题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...

最新文章

  1. 牛顿迭代法求解平方根
  2. python编写自动化脚本 与shell_脚本安装Discuz论坛(shell + Python 实现自动化安装)...
  3. Sublime Text 3 快捷键汇总
  4. LeetCode 153. Find Minimum in Rotated Sorted Array (在旋转有序数组中找到最小值)
  5. DFS-20190206
  6. 及时复盘的好处_如何做好2020的年终复盘?
  7. Leetcode每日一题:1030.matrix-cells-in-distance-order(距离顺序排列矩阵单元格)
  8. Adaptive Placeholder – 自适应的占位符效果
  9. yum mysql 设置密码_Linux下的 Mysql 8.0 yum 安装 并修改密码
  10. mysql grant命令详解_mysql grant命令详解 设置mysql远程连接
  11. 《深入解析Windows操作系统》要点整理
  12. 3.Adaptive AUTOSAR 架构详解
  13. java计算机毕业设计体检系统源码+系统+数据库+lw文档
  14. RoadMap:面向自动驾驶视觉定位的轻量级语义地图(ICRA2021)
  15. 企业级技术与大数据BI——节选自专著《Big Data Fundamentals: Concepts, Drivers Techniques》
  16. 【移动端网页布局】移动端网页布局基础概念 ① ( 移动端浏览器 | 移动端屏幕分辨率 | 移动端网页调试方法 )
  17. 使用C语言教你轻松学会Cantor表
  18. ubuntu18.04 设置字体样式, 调整字体大小
  19. 网站外链优化方案怎么制定
  20. sql 同时(更新)update和(查询)select同一张表

热门文章

  1. 趣谈设计模式 | 单例模式(Singleton) :独一无二的对象
  2. 很现实、很暴力的面试法则 —— 来自招聘官的自述
  3. 揭秘北京2022冬奥会背后的技术
  4. 开启未来十年的编码技术之门
  5. ​FreeSWITCH视频会议“标准”解决方案
  6. Pensieve:AI带来的更流畅的高质量观看体验
  7. 学习多媒体开发的正确姿势?陈功:看书 啃代码 搞插件
  8. 剑指offer之31-35题解
  9. 现代化 C++ 开发工具 CLion 从入门到精通
  10. error: RPC failed; curl 56 GnuTLS recv error (-54): Error in the pull function.