Description

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.

Example

Given [1,2,3] which represents 123, return [1,2,4].

Given [9,9,9] which represents 999, return [1,0,0,0].

解题:此题注意进位即可,具体注意点标注在代码中,代码如下:

public class Solution {
/**
* @param digits: a number represented as an array of digits
* @return: the result
*/
public int[] plusOne(int[] digits) {
// write your code here
boolean[]carry = new boolean[digits.length];//默认false
int n = digits.length;
if(digits[n - 1] !=9){
//无需进位
digits[n - 1]++;
return digits;
}else{
//有进位
for(int i = n-1; i >= 0; i--){
if(digits[i] == 9){
carry[i]=true;
}else{
//很重要
break;
}
}
if(carry[0] == true){
int[]res = new int[n+1];
res[0] = 1;
return res;
}else{
int[]res = new int[n];
for(int i = n - 1; i >= 0; i--){
if(carry[i] == true){
res[i] = 0;
}else if(carry[i] == false && carry[i+1] == true){
res[i] = digits[i] + 1;
}else{
res[i] = digits[i];
}
}
return res;
}
}
}
}

407. Plus One【LintCode java】的更多相关文章

  1. 372. Delete Node in a Linked List【LintCode java】

    Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...

  2. 451. Swap Nodes in Pairs【LintCode java】

    Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...

  3. 445. Cosine Similarity【LintCode java】

    Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...

  4. 433. Number of Islands【LintCode java】

    Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...

  5. 423. Valid Parentheses【LintCode java】

    Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...

  6. 422. Length of Last Word【LintCode java】

    Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ...

  7. 420. Count and Say【LintCode java】

    Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...

  8. 415. Valid Palindrome【LintCode java】

    Description Given a string, determine if it is a palindrome, considering only alphanumeric character ...

  9. 413. Reverse Integer【LintCode java】

    Description Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-b ...

随机推荐

  1. mysql的入门基本使用(适合新手学习)

    登陆Mysql数据库mysql -u root -p 展示(查询)所有数据库show databases;进入数据库use dataname;创建数据库create database dataname ...

  2. BindingException: Invalid bound statement (not found)问题排查:SpringBoot集成Mybatis重点分析

    重构代码,方法抛出异常:BindingException: Invalid bound statement (not found) 提示信息很明显:mybatis没有提供某方法 先不解释问题原因和排查 ...

  3. 从C语言的volatile关键字,了解C#的volatile机制(转载)

    C#中有一个关键字volatile,一直不太明白到底什么时候才用它,只知道在多线程操作同一个变量的时候要使用volatile关键字,下面看到了一篇C语言关于volatile关键字的介绍,写的很不错,其 ...

  4. Truncated class file 问题的解决

    替换class 文件之后出现了 Truncated class file  问题,查找原因,可能是文件损坏,清理缓存可以解决 解决办法: 把tomcat的work目录直接删掉,让他重新启动.rm -r ...

  5. 基于多用户的Oracle数据泵导入导出数据

    登陆SqlPlus: SqlPlus sys/syspwd@MyOrcl AS sysdba 其中:syspwd:sys的登陆密码:MyOrcl:所创建的数据库服务名. 创建数据泵: create o ...

  6. c++基础STL

    今天给大家介绍几个容器,包含的头文件为<vector>,<stack>,<queue>,<map>,<list>,<deque> ...

  7. 【Linux】Linux 的慢动作基础

    了解一下刀片服务器: 刀片服务器是指在高标准度的机架式机箱内插装多个卡式的服务器单元,是一种实现HAHD的低成本服务器平台,其中每一片刀片实际上就是一块系统主板. Linux: Linux操作系统构成 ...

  8. TCC : Tiny C Compiler (2018-2-6)

    饭墙下载,有缘上传: https://files.cnblogs.com/files/bhfdz/tcc-0.9.27-win32-bin.zip https://files.cnblogs.com/ ...

  9. ZYNQ的Linux Linaro系统镜像制作SD卡启动

    ZYNQ的Linux Linaro系统镜像制作SD卡启动 0. 概述 ZYNQ生成uboot的时候和正常的ARM设备不太一样,ZYNQ属于二次辅助启动uboot然后由uboot启动内核,大概意思就是 ...

  10. Python2和Python3

    1. 字符编码 1.1. Python2默认为ACSII编码 1.2. Python3为Unicode 2. Unicode和UTF8和GBK编码的关系 utf8:中文3字节              ...