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. 构建一个hashmap死锁的DEMO

    package threadmodle; import java.util.HashMap; import java.util.Map; import java.util.UUID; public c ...

  2. jquery特效 点击某项,其它隐藏

    <html> <head> </head> <body> <script> $(function(){ $(".cPage a&q ...

  3. ES6中常用新特性讲解

    1.不一样的变量声明:const和let ES6推荐使用let声明局部变量,相比之前的var(无论声明在何处,都会被视为声明在函数的最顶部) let和var声明的区别: var x = '全局变量'; ...

  4. Python 3 字符串转MD5形式

    Python 字符串转MD5: def getStrAsMD5(parmStr): #1.参数必须是utf8 #2.python3所有字符都是unicode形式,已经不存在unicode关键字 #3. ...

  5. spring-quartz 定时器 给targetMethod传递参数

    今天在做一个项目的时候,要给一个定时器任务的执行方法传递参数,在网上找了一下资料,可以使用arguments参数:   <bean id="subsidyJobDetail" ...

  6. STM32F407+STemwin学习笔记之STemwin移植

    原文链接:http://www.cnblogs.com/NickQ/p/8748011.html 环境:keil5.20  STM32F407ZGT6  LCD(320*240)  STemwin:S ...

  7. HDOJ:6333-Problem B. Harvest of Apples(组合数学+莫队算法+逆元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333 解题心得: 这个题可以说是十分精彩了,首先推组合数学的公式,其中一个很重要的公式是Cnm = C ...

  8. 为 ItemsControl 类型的控件提供行号,mvvm模式 绑定集合

    从网络上看到的两种方式,一种是,在 codebehind 里为 控件写事件,下面是将集合绑定到 DataGrid 控件: private void DataGridSoftware_LoadingRo ...

  9. c#调用c++库函数

    如果是非托管的,就用DllImport,举例    using System;    using System.Runtime.InteropServices;    class MainApp    ...

  10. 浅谈Hash在多个字符串匹配类型问题中的应用

    在生活中们有时会遇到一些有关字符串匹配的问题. 这时打暴力往往显得很愚蠢,效率低下. 所以就需要一些算法和数据结构来提高效率. Hash Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把 ...