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 ...
随机推荐
- 我的IIS7.5竟然不支持ASP.NET路由
MVC模式下那些友好,屏蔽具体物理文件的URL让我眼馋,咱也想在WEB FORM项目用上一用. 按照指引,添加global.asax,写上路由代码什么的: <%@ Application Lan ...
- 曲线控件我一直用codeproject上的那几个(C++ 100款开源界面库)
Alberl#23楼[楼主] 2013-11-04 11:47 Alberl @baita00引用看了大神的教程,真的不错,学习了很多东西,^_^.这节教程里,大神好像在找曲线控件,是吗?大神有什特殊 ...
- 国内物联网平台初探(八):中移物联网开放平台OneNet
平台定位 OneNET是中移物联网有限公司搭建的开放.共赢设备云平台,为各种跨平台物联网应用.行业解决方案,提供简便的云端接入.存储.计算和展现,快速打造物联网产品应用,降低开发成本. IoT Paa ...
- Tomcat容器的Session管理
Session管理是JavaEE容器比较重要的一部分,在app中也经常会用到.在开发app时,我们只是获取一个session,然后向session中存取数据,然后再销毁session.那么如何产生se ...
- 10.2 Hibernate持久层
点击项目右键->MyEclipse->Add Hibernate Capabilities 打开MyEclipse Hibernate Perspective(MyEclipse Hibe ...
- 南海区行政审批管理系统接口规范v0.3(规划) 5.投资项目联合审批系统API 5.1.【uploadFile】证件文书附件上传
- encodeURIComponent编码java后台解码出现乱码问题
问题:JavaScript请求后台带着name参数,有中文进行编码:url?name=" + encodeURIComponent(name):java后台直接使用name或者name=ja ...
- linux系统在线搭建禅道
1.先安装wget:yum -y install wget 2.下载安装禅道:[root@zhaowen ~]# wget http://dl.cnezsoft.com/zentao/9.0.1/Ze ...
- BZOJ4518: [Sdoi2016]征途(dp+斜率优化)
Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1875 Solved: 1045[Submit][Status][Discuss] Descript ...
- 【转载】程序猿转型AI必须知道的几件事!
历史上AI火过两次,但是最终都已销声匿迹作为结束.这次AI大火的原因:AlphaGo 4比1战胜李世石,相对于一些外行人的恐慌和恐惧,其实很多业内人员在这场世纪之战结束后,都为人类点上了一个大大的赞. ...