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 ...
随机推荐
- Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds 解决方法
Server Tomcat v6.0 Server at localhost was unable to start within 45 seconds. If the server requires ...
- div自定义的滚动条 (水平导航条)
<!DOCTYPE html> <html> <head> <title></title> <style> div{ /* wi ...
- VC++6.0编译器标记的那些内存值
栈内存初始值 0xcccccccc 和-858993460. 二者是一样的, 一个是16进制, 另一个是10进制
- php课程---面向对象
面向对象:一:定义类 class Dog { var $name; var $age; var $pinzhong; function Jiao() { echo "{$this->n ...
- Thinking in Java——笔记(10)
Inner Classes It allows you to group classes that logically belong together and to control the visib ...
- Docker三剑客之Swarm介绍
DockOne技术分享(二十): 我用swarm在多台物理机调度管理容器,用ovs实现跨主机的容器互联问题 [编者的话]Swarm项目是Docker公司发布三剑客中的一员,用来提供容器集群服务,目的是 ...
- SQL Server 定时自动备份数据库
在SQL Server中出于数据安全的考虑,所以需要定期的备份数据库,这篇文章介绍使用SQL Server 数据库代理中的作业定时自动备份数据库. 1.启动SQL Server代理服务,如下图: 绿色 ...
- ftp列表错误或长城宽带连不上ftp的解决方法
有些是长城宽带,我 帮忙测试,在客户PC机上测试,PING 任何网站 不通:tracert 超时:FTP 超时,不出现用户名提示.但访问网站正常,检测后进入到路由器,禁用DHCP服务 ,问题解决. 或 ...
- perl基础
perl比较好的博客:http://www.cnblogs.com/cosiray/archive/2012/03/18/2404371.html 以分析一个简单的pm文件为例 # # オプションの取 ...
- Oracle的日期时间范围查询
Oracle日期时间范围查询 Sql代码 /* 日期时间范围查询 */ ---------- 创建日期时间测试表-------------------------------------------- ...