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问题) 解题思路和方法的更多相关文章

  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 ...

  2. leetCode 75.Sort Colors (颜色排序) 解题思路和方法

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  3. 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 ...

  4. leetCode 57.Insert Interval (插入区间) 解题思路和方法

    Insert Interval  Given a set of non-overlapping intervals, insert a new interval into the intervals ...

  5. 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 ...

  6. leetCode 67.Add Binary (二进制加法) 解题思路和方法

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  7. leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

  8. leetCode 27.Remove Element (删除元素) 解题思路和方法

    Remove Element Given an array and a value, remove all instances of that value in place and return th ...

  9. leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

    Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...

随机推荐

  1. Spring整合JUnit4测试使用注解引入多个配置文件

    转自:https://kanpiaoxue.iteye.com/blog/2151903 我们使用spring写junit单测的时候,有的时候我们的spring配置文件只有一个.我们在类的注释上面会这 ...

  2. VisoStudio 允许局域网联机调试网站

    第一步:修改配置文件 添加IP访问配置 找到vs访问网站的端口后,添加一行新的配置 第二步:使用CMD命令进行网络配置 netsh http / user=everyone 删除网络配置的命令(注意最 ...

  3. UVA-10347 Medians 计算几何 中线定理

    题面 题意:已知三角形三中线的长度nmp,求面积 题解:如果知道中线定理就比较简单了 三边长为 3*a=sqrt(8*mb*mb+8*mc*mc-4*ma*ma) 3*b=sqrt(8*ma*ma+8 ...

  4. 谈谈cookie的弊端

    一.cookie虽然在数据在客户端持久存储提供了方便,但是分担了服务器数据传输的负担,还是存在很大的局限性的. 局限性: (1)在特定的域名下最多存储20个cookie. 浏览器版本          ...

  5. 3.Ventuz Designer新建项目Demo

    Ventuz Designer新建项目Demo 1.打开ventuz,点Recent Projects>New Project,在弹出的界面填写具体项目信息,如下图: 图1.1 图1.2 2.在 ...

  6. mybatis 高级映射和spring整合之高级映射(4)

    mybatis 高级映射和spring整合之高级映射 ----------------学习结构-------------------- 0.0 对订单商品数据模型进行分析 1.0 高级映射 1.1 一 ...

  7. jquery mobile datepicker

    1.http://jquerymobile.com/demos/1.0a4.1/experiments/ui-datepicker/ 这个只能用在iOS和PC上,使用<input date,An ...

  8. String,StringBuffer,StringBuilder效率优先关系说明

    String,StringBuffer,StringBuilder效率优先关系说明: public class StringBufferWithStringBuilder { public stati ...

  9. 使用QT的一些小Tipster

    1.在使用Qt Creator编程时,难免会用到将float类型转换为QString类型的方法:原文 1.1. 将QString类型转化为float类型,很简单 QString data;       ...

  10. 如何选择优动漫PAINT版本?是个人版还是EX版

    优动漫PAINT也就是我们常说的clip studio paint(CSP)的中文版本,它是一款功能强大的动漫绘图软件,适用于个人和专业团队创作,分为个人版和EX版.搭载了绘制漫画和插画所需的所有功能 ...