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 ...
随机推荐
- mysqlbinglog基于即时点还原
mysqlbinlog介绍 要想从二进制日志恢复数据,你需要知道当前二进制日志文件的路径和文件名.一般可以从选项文件(即my.cnf or my.ini,取决于你的系统)中找到路径. (mysql5. ...
- 从jQuery中学习来的另一种继承方式(技巧)
遵从Js的原型链规则,利用js灵活的特性灵活地改造原型,可以实现各种创意地继承方式,昨天研究了jQuery,对作者实现继承的方式感到佩服,他对js原型和原型链的理解比较透彻,运用自如.这里给出jQue ...
- $_POST 变量以及$GLOBALS['HTTP_RAW_POST_DATA']
$_POST 变量是一个数组,内容是由 HTTP POST 方法发送的变量名称和值. $_POST 变量用于收集来自 method="post" 的表单中的值.从带有 POST 方 ...
- 理解insert all/insert first的使用
在常用的SQL写法中我们会经常遇到把一个表的数据插入另外一张表的情况,这是一个insert into 表名 select .... from 表名 就可以解决了.但是如果是把一张表的数据同时插入两 ...
- Mysql 性能调优之Memory 计算
最近在做mariadb 数据库性能调优时发现,配置文件影响着整个数据库的性能的百分之80(这么说不为过),现在就我出现的问题来分析. 在压测mariadb时,tail 日志.发现压测到一半 ,数据库会 ...
- php全面获取url地址栏及各种参数
<?php echo $_SERVER['HTTP_HOST']."<br>";//获取域名或主机地址 echo $_SERVER["SERVER_PO ...
- # 20145334 《Java程序设计》第9周学习总结
20145334 <Java程序设计>第9周学习总结 教材学习内容总结 第十六章 整合数据库 JDBC 1.Java语言访问数据库的一种规范,是一套API. 2.JDBC (Java Da ...
- BizTalk开发系列(三十二)浅谈BizTalk主机性能优化
很多BizTalk的项目都要考虑到性能优化的问题,虽然BizTalk采用多线程处理消息的,大大提高了程序效率.但默认情况下 BizTalk的主机有很多阻止参数会控制BizTalk对服务器的资源使用率, ...
- vim的常用命令
平常最多是用vim来编辑单个文件,看看源码.就是写几k行代码时也没有用一些其他的插件,只是设置了高亮等一些自带的属性.这样的好处是,换到任何一台新机上都能立马使用. 网上流传了大量的“vim命令合集” ...
- win7安装virtualbox遇到的问题
今天用台式机的时候想装个virtualbox跑centos做测试用,结果centos始终装不上,vbox一直提示无法开启任务.重装vbox,以及手动点击安装xxx.inf文件,都不行. 以前用的win ...