LeetCode----66. Plus One(Java)
package plusOne66;
/*
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.
*/
public class Solution { public static int[] plusOne(int[] digits) {
int index=digits.length-1;
digits[index]=digits[index]+1;
while(digits[index]>=10&&index>0){
digits[index]=digits[index]%10;
index--;
digits[index]=digits[index]+1;
}
if (digits[0]>9)
{
digits[0]=digits[0]%10;
int[] result=new int[digits.length+1];
result[0]=1;
for (int i=0;i<digits.length;i++){
result[i+1]=digits[i];
}
return result;
}
return digits;
}
public static void main(String[] args) {
// TODO Auto-generated method stub int[] digits={9,9,9,9};
//System.out.println(plusOne(digits).toString());
} }
LeetCode----66. Plus One(Java)的更多相关文章
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- 前端与算法 leetcode 66. 加一
目录 # 前端与算法 leetcode 66. 加一 题目描述 概要 提示 解析 解法一 解法二 算法 # 前端与算法 leetcode 66. 加一 题目描述 给定一个由整数组成的非空数组所表示的非 ...
- Java实现 LeetCode 66 加一
66. 加一 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示 ...
- Java [Leetcode 66]Plus One
题目描述: Given a non-negative number represented as an array of digits, plus one to the number. The dig ...
- [LeetCode]66.加一(Java)
原题地址: plus-one 题目描述: 给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 ...
- leetcode 刷题记录(java)-持续更新
最新更新时间 11:22:29 8. String to Integer (atoi) public static int myAtoi(String str) { // 1字符串非空判断 " ...
- LeetCode 66. Plus One(加1)
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. Yo ...
随机推荐
- backbone学习笔记一
backbone是一个MVC单页面框架,针对传统的WEB开发B/S架构的缺点,即通过表现层的浏览器,功能层的WEB服务器,数据层的数据库服务器构架,而操作渲染过程太过复杂.
- asp.net 获取当前项目的根目录路径
获取网站根目录的方法有几种如: Server.MapPath(Request.ServerVariables["PATH_INFO"]) Server.MapPath(" ...
- spring security自定义拒绝访问页面
如果试图访问一个没有访问权限的页面,那么页面会出现403 访问错误. 如果我们希望自定义访问拒绝页面,只需要随便创建一个jsp页面,让后将这个页面的位置放到配置文件中. 下面创建一个accessDen ...
- sprintf的缓冲区溢出问题
因为sprintf函数没有参数指定缓冲区的大小,这使得溢出的可能性很大,尤其是遇到 sprintf( buffer, "%s", a ) 如果不知道a的串长,就无法指定安全的缓冲区 ...
- iOS 状态栏黑色背景白色字体
一. 状态栏背景(黑色)的设置 1.在有导航栏的情况下,给导航栏设置一个像素为44的背景图片即可 [[UINavigationBar appearance] setBackgroundImage:[U ...
- TFTP服务
$ mkdir /tftpboot$ cp exynos4412-fs4412.dtb uImage /tftpboot$ sudo chmod 777 tftpboot(~下)$ sudo chmo ...
- Myeclipse8.5 subscription expired自己动手获取Myeclipse的注册码
步骤: 1.在myeclipse中新建一个java project 2.在src目录下建立一个名为MyEclipseGen的类 3.将下面的代码复制到该类中,并运行. import java.io.* ...
- vim正则表达式(转)
Vim中的正则表达式功能很强大,如果能自由运用,则可以完成很多难以想象的操作. 如果你比较熟悉Perl的正规表达式,可以直接参照与Perl正则表达式的区别一节. 一.使用正则表达式的命令 使用正则表达 ...
- vba 工作案例1
手上有一份关于广东22个地市的数据,行列不符合预期的表结构,稍vba转换下,再text import 到oracle. Sub copy() ' ' copy 宏 ' ' 快捷键: Ctrl+Shif ...
- [archlinux][hardware] 查看SSD的使用寿命
因为最近把16GB的SSD做成了HDD的cache,所以比较关系寿命问题. 使用smartctl工具. 参考:https://www.v2ex.com/t/261373 linux 下面只有 smar ...