407. Plus One【LintCode java】
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】的更多相关文章
- 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 ...
- 451. Swap Nodes in Pairs【LintCode java】
Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...
- 445. Cosine Similarity【LintCode java】
Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...
- 433. Number of Islands【LintCode java】
Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...
- 423. Valid Parentheses【LintCode java】
Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...
- 422. Length of Last Word【LintCode java】
Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ...
- 420. Count and Say【LintCode java】
Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...
- 415. Valid Palindrome【LintCode java】
Description Given a string, determine if it is a palindrome, considering only alphanumeric character ...
- 413. Reverse Integer【LintCode java】
Description Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-b ...
随机推荐
- CSU - 1224 ACM小组的古怪象棋
传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1224 1224: ACM小组的古怪象棋 Lime Limit: 1 Sec ...
- UVALive4682 XOR Sum
UVALive4682 XOR Sum 题意 给定一个数组, 求连续子序列中异或值最大的值. 题解 假设答案区间为 [L, R], 则答案为 XOR[L, R], 可以将区间分解为 XOR[L,R] ...
- 2.java编辑器和java大致内容
离开了宇宙第一IDE.对java的编辑的选择有点茫然. .net只有一个你不用选择.java好几个.对于追求完美的我来说.总想选个完美的.上网百度可一下.最经典的当然是eclipse了. 但是觉得有点 ...
- 产线事故:删除创建mysql索引
单表数据量:670W: 删除一个老的索引,新建一个新的索引. 事故原因: 先删除索引,应用访问量大,没有索引自然慢,数据库CPU飚到100%:新索引创建失败. 直接造成交易打烊. 日志: ------ ...
- Oracle 11g密码过期问题
Oracle 11g默认用户密码会在使用180天后过期,我们可以通过dba_users数据字典看一下用户的信息. SQL> select username,account_status,lock ...
- Centos6_32位系统512M内存_如何安装gogs_Mysql_配置开机自启动
因为有很多人的Linux版本比较低,内存配置也较低,X86 ,32位系统的:所以这里推荐采用二进制安装gogs,并且使用Mysql:这个是傻瓜式的安装方案,适合绝大多数人(提及了centos7的安装思 ...
- Tomcat性能监控
Tomcat性能监控工具很多,这里介绍两种1.JMeter 2.probe,使用这两种工具都需要在tomcat的安装目录/conf/tomcat-users.xml添加 <tomcat-user ...
- TinyMCE插件:Filemanager [4.x-6.x] 图片自动添加水印
上传图片程序(filemanager/upload.php) 在if (!empty($_FILES) && $upload_files)有一个move_uploaded_file() ...
- Laravel 入门笔记
1.MVC简介 MVC全名是Model View Controller,是模型-视图-控制器的缩写 Model是应用程序中用于处理应用程序数据逻辑的部分 View是应用程序中处理数据显示的部分 Con ...
- Invoice Helper
using System; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using Microsoft.Crm.Sdk.Messag ...