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. POJ 3122 Pie(二分+贪心)

    Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22684   Accepted: 7121   Special Ju ...

  2. 轻量ORM-SqlRepoEx (十一)扩展

    以下为 SqlRepoEx.MsSql.ServiceCollection 代码 public static IServiceCollection AddSqlRepo(this IServiceCo ...

  3. 自己封装了的AlertController

    一直觉得使用系统这个东西写起来特别麻烦,每次都要写一大推东西,还是重复的,今天抽了点时间自己重新封装了一下,解决了自己的强迫症...,不多说,直接上代码了. 1.自己定义了一个名为XBZ的UIAler ...

  4. 几行代码实现iOS摇一摇功能

    实现这个功能很简单,我们直接看代码 - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{ NSLog(@&quo ...

  5. Android软件开发之SharedPreferences

    SharedPreferences 一种轻量级的数据保存方式 以键值对的方式存储 用于存储小批量的数据   使用方法: SharedPreferences sp= getSharedPreferenc ...

  6. mui的openWindowWithTitle()参数及说明

    mui.openWindowWithTitle({ url: 'xxx.html', //String类型,要打开的界面的地址 id: 'id', //String类型,要打开的界面的id style ...

  7. 学习笔记 - Manacher算法

    Manacher算法 - 学习笔记 是从最近Codeforces的一场比赛了解到这个算法的~ 非常新奇,毕竟是第一次听说 \(O(n)\) 的回文串算法 我在 vjudge 上开了一个[练习],有兴趣 ...

  8. python函数基本介绍

    函数 1.函数结构 def 是函数的定义关键字,my_len是函数名.()传参用,冒号下面都是函数体. 执行函数方法:函数名加括号来执行函数.My_len() 举例: # s = 'lkfjsjulk ...

  9. 关于document.write()加载JS等静态资源 和 异步async加载JS

    现流行浏览器对于静态资源的预加载 传统的浏览器,对于静态资源加载,会阻塞 HTML 解析器的线程进行,无论内联还是外链. 例如: <script src="test1.js" ...

  10. jQuery树形控件zTree使用小结

    作者:Fonour 字体:[增加 减小] 类型:转载 时间:2016-08-02我要评论 这篇文章主要为大家详细介绍了jQuery树形控件zTree使用方法,zTree树插件的基本使用方法,感兴趣的小 ...