66. Plus One
2016-01-15 11:37
211 查看
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.
The digits are stored such that the most significant digit is at the head of the list.
public class Solution { public int[] plusOne(int[] digits) { if(digits == null || digits.length == 0) return new int[]{1}; int l = digits.length; int add = (digits[l-1] + 1)/10; digits[l-1] = (digits[l-1] + 1)%10; int i = l-2; for( ; i >= 0 && add > 0;i--){ int tmp = digits[i] + add; add = tmp/10; digits[i] = tmp%10; } if(add == 0) return digits; int[] r = new int[l+1]; r[0] = 1; for(i=1 ; i <= l;i++){ r[i] = digits[i-1]; } return r; } }
相关文章推荐
- nginx屏蔽IP
- 在Win7虚拟机下搭建Hadoop2.5.2+Spark1.5.2单机环境
- console编码问题
- Java将字节转换为十六进制代码分享
- cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
- ios植入广告
- 透明度
- java中数字类型转换
- Android开发路线图
- 修改Android EditText光标颜色
- Hibernate 映射组件属性
- RabbitMQ入门教程 For Java【2】 - Work Queues
- 【转】Android应用程序常见问题
- 不需要经历也能明白——理性的力量
- 2416开发记录二:最简单的helloworld驱动
- foxmail设置gmail 不翻墙,切换账户!!!thunderbird
- linux基础命令之xargs
- 第一个博客:数组
- PARSEC-3.0编译错误
- implicitly declaring library function 'objc_msgSend'with type '(id,SEL,...)' 警告