leetCode 66.Plus One (+1问题) 解题思路和方法
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的值。主要就是进位的问题。代码例如以下:
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;
}
}
leetCode 66.Plus One (+1问题) 解题思路和方法的更多相关文章
- leetCode 86.Partition List(分区链表) 解题思路和方法
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- leetCode 75.Sort Colors (颜色排序) 解题思路和方法
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- leetCode 15. 3Sum (3数之和) 解题思路和方法
3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...
- leetCode 57.Insert Interval (插入区间) 解题思路和方法
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ...
- leetCode 61.Rotate List (旋转链表) 解题思路和方法
Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For ex ...
- leetCode 67.Add Binary (二进制加法) 解题思路和方法
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- leetCode 27.Remove Element (删除元素) 解题思路和方法
Remove Element Given an array and a value, remove all instances of that value in place and return th ...
- leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
随机推荐
- tiny4412开机动画、开机界面的定制 【原创】
关键词:Android linux 开机logo 开机动画 平台信息:内核:linux3.0.68 系统:android/android5.1平台:tiny4412 作者:庄泽彬(欢迎转载,请注明 ...
- SQLite 常用函数
SQLite 常用函数 参考: SQLite 常用函数 | 菜鸟教程http://www.runoob.com/sqlite/sqlite-functions.html SQLite 常用函数 SQL ...
- JavaScript:DOM对象
ylbtech-JavaScript:DOM对象 1. HTML DOM Document 对象返回顶部 1. HTML DOM Document 对象 HTML DOM 节点 在 HTML DOM ...
- 院校-国外-美国:斯坦福大学( Stanford)
ylbtech-院校-国外-美国:斯坦福大学( Stanford) 斯坦福大学(Stanford University),全名小利兰·斯坦福大学(Leland Stanford Junior Univ ...
- BZOJ 1041 数学
思路: $x^2+y^2=r^2$$y=\sqrt{(r+x)(r-x)}$令$ d=gcd(r+x,r-x)$设A=$(r-x)/d$ $B=(r+x)/d$则$gcd(A,B)=1$$y^2=d^ ...
- JS排序之冒泡排序
冒泡排序的两种策略: <script>// 第一种思路:// 一个数组中的数据,拿第一个和剩下的依次进行对比,数值小的赋值给第一个,一轮比较过后,则数值小的放在最前边.// 第二轮比较,则 ...
- poj1111 Image Perimeters 广搜
题目大意: 输入一个矩阵,再输入其中一个“X”的位置(从1开始).从该位置向八个方向扩展,如果是“X”就可以并在一起.问最后得到的模块的周长是多少. 解题思路: 按照广搜的思路来做.用一个二维的数组标 ...
- 【Oracle】利用trace文件重建控制文件
重建步骤: 1.生成trace文件: alter database backup controlfile to trace; 2.找到生成的trace文件: show parameter dump u ...
- es6学习 http://es6.ruanyifeng.com/
基础学习 http://es6.ruanyifeng.com/ 够了 1字符串 字符串的遍历器接口 for (let codePoint of 'foo') { console.log(code ...
- 谷歌C++编程为何禁止缺省参数
C++的缺省参数尽量不要使用,结果可能出乎我们的意料,下面的程序大家看看输出结果是多少? ) cout << num << endl; ...