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. linux下图片转换工具[【转】

    本文转载自:https://linux.cn/article-8014-1.html 计算机术语中,批处理指的是用一个非交互式的程序来执行一序列的任务的方法.这篇教程里,我们会使用 Linux 命令行 ...

  2. MongoDB Master-Slave cluster with authentication setup

    Master Server create mongo db folder with sub folders like data, conf, && log mkdir -p /opt/ ...

  3. iOS数组越界

    数组越界就是假如你的下标总数现在为32个,然后你在下一秒又执行了一个方法要从50个数据里进行赋值啊筛选之类的,而你此时数组里的值为32个,50的数据还没有请求到,往往会出现数组越界的崩溃信息,大概是这 ...

  4. OCC 矩阵变换

    在OpenCADCADE中, 通过gp_Trsf类来进行矩阵变换操作, 采用矩阵在左的方式: 新点 = 变换矩阵 * 点 基本原理如下: //! Defines a non-persistent tr ...

  5. 【Oracle】删除undo表空间时,表空间被占用:ORA-30042: Cannot offline the undo tablespace

    特别注意:此办法只用于实在没有办法的时候,因为需要加入oracle中的隐含参数,慎用!!! 1. 先查一下是什么在占用undo SYS@ENMOEDU>select segment_name,o ...

  6. FBX骨骼坐标系与模型坐标系的关系

    采用assimp加载FBX文件.首先记录下ubuntu下assimp的编译安装. cd assimp_unzip_dir mkdir build cd build && cmake . ...

  7. 考考你对java多态的理解

    请看如下代码, 如果你能不运行得出正确答案, 那你真的超过99%的java程序员了. [本人属于最大头的那部分] public class A{ public String s = "A&q ...

  8. Functor、Applicative 和 Monad(重要)

    Functor.Applicative 和 Monad Posted by 雷纯锋Nov 8th, 2015 10:53 am Functor.Applicative 和 Monad 是函数式编程语言 ...

  9. Java中成员变量和局部变量区别

    在类中的位置不同 重点 成员变量:类中,方法外 局部变量:方法中或者方法声明上(形式参数) 作用范围不一样 重点 成员变量:类中 局部变量:方法中 初始化值的不同 重点 成员变量:有默认值 局部变量: ...

  10. Ubuntu 18.04 安装 Xfce桌面和VNC的方法

    首先安装Xfce4桌面环境.Xfce4是在Unix和Unix-like(Linux, FreeBSD)上运行的开源桌面环境,其特点是快速,轻量,同时拥有美观的交互界面,易于使用. Xfce4的安装十分 ...