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 ...
随机推荐
- c++构造函数详解(转)
c++构造函数的知识在各种c++教材上已有介绍,不过初学者往往不太注意观察和总结其中各种构造函数的特点和用法,故在此我根据自己的c++编程经验总结了一下c++中各种构造函数的特点,并附上例子,希望对初 ...
- CSS Variables
CSS原生变量(CSS自定义属性) 示例地址:https://github.com/ccyinghua/Css-Variables 一.css原生变量的基础用法 变量声明使用两根连词线"-- ...
- Python字符串必记函数
Python字符串函数数不胜数,想要记完所有几乎不可能,下列几个是极为重要的一些函数,属于必记函数. 一.join 功能: 将字符串.元组.列表中的元素以指定的字符(分隔符)连接生成一个新的字符串 语 ...
- MySQL数据库的原理
点进去就是你历经千辛万苦找到的数据库的原理: https://www.cnblogs.com/smallyard/p/5626061.html
- Python在线编程环境
除了安装Python的IDE之外,也可以使用在网页中随时随地编写Python程序. Python官网:https://www.python.org/shell Python123:https://py ...
- C语言Windows程序开发—MessageBox函数介绍【第01天】
(一)MessageBox函数的参数介绍: int MessageBox ( HWND hWnd, //弹出MessageBox对话框所属的窗口句柄 LPCTSTR lpText, //指向Messa ...
- 状压搜索 洛谷T47092 作业
TYM 有 nn 本作业,编号为 1,\dots,n1,…,n. 由于 \mathrm{TYM}TYM 很喜欢偷懒,而且不喜欢消耗脑细胞,所以他选择跳着完成这 nn 本作业.此外,如果将做作业的顺序转 ...
- BZOJ4247_挂饰_KEY
题目传送门 背包的变形,不得不说卡了我很久(估计是下午睡傻了). 设f[i][j]为前i个物品剩下j个挂钩. f[i][j]=max(f[i-1][j],f[i-1][max(j-a[i].x,0)+ ...
- USACO16OPEN_248&&USACO16OPEN_262144_KEY
题目传送门 这道题比较水,设f[i][j]表示i~j区间合并的最大值. #include <cstdio> #define max(a,b) a>b?a:b using namesp ...
- Java:内存泄露和内存溢出
1. 内存溢出 (Memory Overflow) 是指程序在申请内存时,没有足够的内存空间供其使用,出现out of memory:比如申请了一个integer,但给它存了long才能存下的数,那就 ...