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. POJ3204 Ikki's Story - Road Reconstruction 网络流图的关键割边

    题目大意:一个有源有汇的城市,问最少增加城市中的多少道路可以增加源到汇上各个路径上可容纳的总车流量增加. 网络流关键割边集合指如果该边的容量增加,整个网络流图中的任意从原点到汇点的路径的流量便可增加. ...

  2. Android常用的一些make命令【转】

    本文转载自:http://blog.csdn.net/liuxd3000/article/details/39181377 1.make -jX  X表示数字,这个命令将编译Android系统并生成镜 ...

  3. AIX&nbsp;常用命令汇总(一)

    命令 内核 如何知道自己在运行 32 位内核还是 64 位内核? 要显示内核启用的是 32 位还是 64 位,可输入以下命令: bootinfo -K 如何知道自己在运行单处理器还是多处理器内核? / ...

  4. Pinpoint 监控

    ####Hbase数据################ 参考: 然而没有卵用: https://blog.csdn.net/iamlihongwei/article/details/52882749? ...

  5. Socket server

    Socket server的使用方法(精华部分),仅供自用. class MyServer(socketserver.BaseRequestHandler): def handle(self): wh ...

  6. 从发请求到AJAX到同源政策

    1  发请求的各种方法 使用form标签(会在当前页面刷新或者新开一个页面刷新) <form action="" method=post/get> <input ...

  7. JavaScript获取非行间样式

    <html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...

  8. Qt与OpenCV结合:图像显示

    参考链接: http://www.cnblogs.com/emouse/archive/2013/03/29/2988717.html 注意:为了防止min max 问题,要把opencv的包含放在包 ...

  9. Swift 中实现 Promise 模式

    在异步编程中,除了竟态处理.资源利用以外,另外一个难点就是流程管理.在拥有匿名函数.闭包这些特性的编程语言中,我们通常可以使用回调函数来做一个异步任务完成或失败时的处理.但当我们的业务逻辑逐渐复杂时, ...

  10. Linux学习之路三:重要概念之Linux系统层次结构

    上图来自Unix编程圣经<APUE>英文第二版.如图,处于最中心的是系统内核,负责机器硬件资源管理,进程管理等:shell,函数库(值得记住的是C标准函数库)和某些应用程序均直接构建于内核 ...