Description

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 in the array contain a single digit.

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

Example 1:

Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.

Example 2:

Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.

  题目理解:输入一个数组,数组中的每个元素都不大于10,每一个元素相当于一个正整数每一位的数,输出这个正整数加一之后的数,用数组表示

  分析:1,如果当前为不为9则直接ji当前位加1即可

     2,如果为9 则当前位置为0,继续1的操作。

我的解决方法

public class Solution {
public int[] PlusOne(int[] digits) {
int[] result = null;
int i = digits.Length - ;
while (i >= && digits[i] + == )
{
digits[i] = ;
i--;
}
if (i >= )
{
digits[i] = digits[i] + ;
return digits;
}
result = new int[digits.Length +]; result[] = ;
return result; }
}

288ms

另一种更好的解决方案(264ms)

public class Solution {
public int[] PlusOne(int[] digits) { int n = digits.Length; for(int i= n-; i>=;i--){
if(digits[i]<){
digits[i]++;
return digits;
}
digits[i]= ;
} int[] answer = new int[n+];
answer[] = ;
return answer;
}
}

LeetCode Array Easy 66. Plus One的更多相关文章

  1. LeetCode Array Easy 88. Merge Sorted Array

    Description Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted ar ...

  2. LeetCode Array Easy 485. Max Consecutive Ones

    Description Given a binary array, find the maximum number of consecutive 1s in this array. Example 1 ...

  3. LeetCode Array Easy 448. Find All Numbers Disappeared in an Array

    Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear ...

  4. LeetCode Array Easy 414. Third Maximum Number

    Description Given a non-empty array of integers, return the third maximum number in this array. If i ...

  5. LeetCode Array Easy 283. Move Zeroes

    Description Given an array nums, write a function to move all 0's to the end of it while maintaining ...

  6. LeetCode Array Easy 268. Missing Number

    Description Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one th ...

  7. LeetCode Array Easy 219. Contains Duplicate II

    ---恢复内容开始--- Description Given an array of integers and an integer k, find out whether there are two ...

  8. LeetCode Array Easy 217. Contains Duplicate

    Description Given an array of integers, find if the array contains any duplicates. Your function sho ...

  9. LeetCode Array Easy 189. Rotate Array

    ---恢复内容开始--- Description Given an array, rotate the array to the right by k steps, where k is non-ne ...

随机推荐

  1. firefox浏览器强制取消自动更新

    问题:Firefox浏览器,在浏览器的设置中已经设置了取消自动升级,实际退出Firefox浏览器重新启动浏览器后还是会升级到最新版本.影响:Firefox浏览器不同的版本的插件的支持兼容不一样,如果需 ...

  2. kvm学习笔记(一,基础概念)

    前言 阿里云的云主机,采用的kvm,今天就花了点时间研究了下. 一.安装 官方文档参考:http://www.linux-kvm.org/page/HOWTO 二.快速建立一个基于vnc的虚拟机安装 ...

  3. 有趣的taskset命令,使进程再指定CPU上运行

    前言 taskset命令,用于进程的CPU调优,可以把某进程,指定再某CPU内工作. 如还不明白,可以参考此文 http://www.361way.com/linux-context-switch/5 ...

  4. 解决VMwave下卡死的办法

    在VMwave路径下找到vmwave.log文件: 如上图所示:在资源监视器中找到name = vmwave-vmx.exe ,pid = 5940的进程,然后杀死.

  5. JavaSE---IO体系

    1.BIO 1.1 Block IO,同步阻塞IO: 1.2 eg:java.io   包下的      InputStream . OutputStream.  Writer.Reader... j ...

  6. read(),readline() 和 readlines() 比较

    read(),readline() 和 readlines() 比较 共同点:均可接受一个变量用以限制每次读取的数据量,但通常不使用 区别: read() [即 fileObject().read( ...

  7. Mybatis缓存+配置

    mybatis提供了缓存机制减轻数据库压力,提高数据库性能 mybatis的缓存分为两级:一级缓存.二级缓存 一级缓存是SqlSession级别的缓存,缓存的数据只在SqlSession内有效 二级缓 ...

  8. 木棍加工(dp,两个参数的导弹拦截问题)

    题目描述 一堆木头棍子共有n根,每根棍子的长度和宽度都是已知的.棍子可以被一台机器一个接一个地加工.机器处理一根棍子之前需要准备时间.准备时间是这样定义的:     第一根棍子的准备时间为1分钟:   ...

  9. jenkins安装-配置

    jenkins安装-配置 注意: jenkins访问 用chrome浏览器 安装包下载:http://pkg.jenkins-ci.org/redhat/ (使用2.92版本的) 安装jdk: 1.8 ...

  10. Java IO Demo

    //FileReader FileWriter 读写英文    public void FileReaderAndWriter1() throws Exception { File filePath ...