Question

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.

Solution

To solve this problem, we can use a flag to mark if the current digit needs to be changed.

Time complexity O(n), space cost O(n)

 public class Solution {
public int[] plusOne(int[] digits) {
int length = digits.length;
int[] result = new int[length + 1];
int flag = 1;
for (int i = length - 1; i >= 0; i--) {
if (flag == 1) {
if (digits[i] < 9) {
digits[i] += 1;
flag = 0;
} else {
digits[i] = 0;
}
}
}
if (flag == 1) {
for (int i = 0; i < length; i++)
result[i + 1] = digits[i];
result[0] = 1;
return result;
}
return digits;
}
}

Plus One 解答的更多相关文章

  1. 精选30道Java笔试题解答

    转自:http://www.cnblogs.com/lanxuezaipiao/p/3371224.html 都 是一些非常非常基础的题,是我最近参加各大IT公司笔试后靠记忆记下来的,经过整理献给与我 ...

  2. 精通Web Analytics 2.0 (8) 第六章:使用定性数据解答”为什么“的谜团

    精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 第六章:使用定性数据解答"为什么"的谜团 当我走进一家超市,我不希望员工会认出我或重新为我布置商店. 然而, ...

  3. 【字符编码】Java字符编码详细解答及问题探讨

    一.前言 继上一篇写完字节编码内容后,现在分析在Java中各字符编码的问题,并且由这个问题,也引出了一个更有意思的问题,笔者也还没有找到这个问题的答案.也希望各位园友指点指点. 二.Java字符编码 ...

  4. spring-stutrs求解答

    这里贴上applicationContext里的代码: <?xml version="1.0" encoding="UTF-8"?> <bea ...

  5. JavaScript Bind()趣味解答 包懂~~

    首先声明一下,这个解答是从Segmentfault看到的,挺有意思就记录下来.我放到最下面: bind() https://developer.mozilla.org/zh-CN/docs/Web/J ...

  6. CMMI4级实践中的5个经典问题及解答

    这五个问题相当经典而且比较深,需要做过CMMI4.5级的朋友才能看懂这些问题.这5个问题是一位正在实践CMMI4级的朋友提出来的,而解答则是我的个人见解. 五个疑问是:   A.流程,子流程部分不明白 ...

  7. 海边直播目标2017全国初中数学竞赛班课堂测试题解答-The Final

    1. 设函数 $f(x) = 2^x(ax^2 + bx + c)$ 满足等式 $f(x+1) - f(x) = 2^x\cdot x^2$, 求 $f(1)$. 解答: 由 $f(x) = 2^x( ...

  8. 知乎大牛的关于JS解答

    很多疑惑一扫而空.... http://www.zhihu.com/question/35905242?sort=created JS的单线程,浏览器的多进程,与CPU,OS的对位. 互联网移动的起起 ...

  9. [问题2014A01] 解答一(第一列拆分法,由张钧瑞同学提供)

    [问题2014A01] 解答一(第一列拆分法,由张钧瑞同学提供) (1)  当 \(a=0\) 时,这是高代书复习题一第 33 题,可用升阶法和 Vander Monde 行列式来求解,其结果为 \[ ...

  10. [问题2014A01] 解答二(后 n-1 列拆分法,由郭昱君同学提供)

    [问题2014A01] 解答二(后 n-1 列拆分法,由郭昱君同学提供) \[|A|=\begin{vmatrix} 1 & x_1^2-ax_1 & x_1^3-ax_1^2 &am ...

随机推荐

  1. 3 Sum 解答

    Question Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Fi ...

  2. 关于qt学习的一点小记录(1)

    今日为了应付学校作业要求 决定现学qt来制作界面 毕竟c++不像在这方面c#可以那么方便 qt主要依靠信号.槽来实现类似winform中的消息 鉴于要尽快做完,故而没有细看qt 只是大概了解了下界面的 ...

  3. The method setOnClickListener(View.OnClickListener) in the type View is not applicable

    开始学习 android 了,学习的是高明鑫老师的android视频教程(android视频教学). 学到第八讲时, 在写动态设置时报错: The method setOnClickListener( ...

  4. redis知识

    http://www.cnblogs.com/moon521/p/5301895.html 菜鸟教程:http://www.runoob.com/redis/redis-tutorial.html

  5. (转)25个增强iOS应用程序性能的提示和技巧--初级篇

    在开发iOS应用程序时,让程序具有良好的性能是非常关键的.这也是用户所期望的,如果你的程序运行迟钝或缓慢,会招致用户的差评.然而由于iOS设备的局限性,有时候要想获得良好的性能,是很困难的.在开发过程 ...

  6. [Immutable.js] Lightning Fast Immutable.js Equality Checks with Hash Codes

    While Immutable.js offers .is() to confirm value equality between iterables it comes at the cost of ...

  7. android studio github 项目导入问题

    在github上面看到一个比较好的项目,导入出现了一些问题,记录如下: 项目演示效果如图:下载地址:https://github.com/asijack/PagerSlidingTabStrip 如果 ...

  8. Java中遍历Map对象的方法

    方法一: 在for-each循环中使用entries来遍历 这是最常见的遍历方式,在需要获取key和value时使用. Map<Integer, Integer> map = new Ha ...

  9. 适用于CSS2的各种运动的javascript运动框架

    <script> window.onload = function() { //var oDiv1 = document.getElementById('box1'); //var oDi ...

  10. .responsiveSlides参数

    $(".rslides").responsiveSlides({ auto: true, // Boolean: Animate automatically, true or fa ...