leetCode 66.Plus One (+1问题) 解题思路和方法
2015-07-17 16:32
344 查看
Plus One
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.
思路:给定一个数组,表示一个数,然后返回+1的值。主要就是进位的问题,代码如下:
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.
思路:给定一个数组,表示一个数,然后返回+1的值。主要就是进位的问题,代码如下:
public class Solution { public int[] plusOne(int[] digits) { int k = 1;//进位 for(int i = digits.length - 1; i >=0 ; i--){ digits[i] += k;//加上进位的值 k = digits[i]/10;//进位值 digits[i] %= 10;//留下的值 } //还有进位 if(k > 0){ int[] a = new int[digits.length+1]; a[0] = k; for(int i = 0; i < digits.length; i++){ a[i+1] = digits[i]; } return a; } return digits; } }
相关文章推荐
- 守护进程
- The Unsolvable Problem
- 序列化魔术函数__sleep()和反序列化魔术函数__wakeup()
- javascript学习笔记之DOM与表单
- DFS与BFS 的区别
- opensips添加自定义数据库表(mysql)
- 用sysctl调整linux内核参数
- UiAutomator
- codevs1003
- Linux PS 命令详解
- GMM-HMM学习笔记
- 【Android Studio探索之路系列】之四:Android Studio快捷键
- iOS事件拦截及应用
- 开博啦
- TIOBE 2015年7月编程语言排行榜:C++的复兴
- Ohana Cleans Up
- hadoop笔记(二)
- 搜索路上的一把辛酸泪,入门搜索题
- 3、iOS传值的方法-Notifaction正向传值
- mysql转换UNIX时间戳为标准日期格式