66. Plus One

Easy

Given a non-empty array of digits representing a non-negative integer, plus one to the integer.

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.

Example 1:

Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.

Example 2:

Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
package leetcode.easy;

public class PlusOne {
@org.junit.Test
public void test() {
int[] digits11 = { 1, 2, 3 };
int[] digits21 = { 4, 3, 2, 1 };
int[] digits12 = plusOne(digits11);
int[] digits22 = plusOne(digits21); for (int i = 0; i < digits12.length; i++) {
System.out.print(digits12[i]);
}
System.out.println(); for (int i = 0; i < digits22.length; i++) {
System.out.print(digits22[i]);
}
System.out.println();
} public int[] plusOne(int[] digits) {
for (int i = digits.length - 1; i >= 0; i--) {
if (digits[i] != 9) {
digits[i]++;
break;
} else {
digits[i] = 0;
}
} if (digits[0] != 0) {
return digits;
} else {
int[] result = new int[digits.length + 1];
result[0] = 1;
return result;
}
}
}

LeetCode_66. Plus One的更多相关文章

随机推荐

  1. Android adb的一些用法

    adb查看包名/Activity名 adb shell "logcat | grep START" adb shell dumpsys activity | find “mFocu ...

  2. 24 结合webpack使用vue-router

    启用路由 参考官网:https://router.vuejs.org/zh/installation.html webpack就是一个模块化的工具 安装 cnpm i vue-router -S

  3. Ubuntu增加swap交换空间的步骤

    1.首先用命令free查看系统内 Swap 分区大小. free -m total used free shared buffers cached Mem: 2012 1960 51 0 748 95 ...

  4. Oracle递归查询connect by

    一.概述 Oracle中可以通过START WITH . . . CONNECT BY . . .子句来实现SQL的层次查询. 自从Oracle 9i开始,可以通过 SYS_CONNECT_BY_PA ...

  5. Mybatis接口中传递多个参数

    1.接口 public interface MemberMapper { public boolean insertMember(Members member); public Members sel ...

  6. WebStorm 简单搭建NodeJs服务

    开始使用 WebStorm 搭建( WebStorm 请自行安装...... ) 在 项目 根目录 新建个 app.js 开始 编写 app,js // 引入 HTTP 模块 const http = ...

  7. 使用jQuery快速高效制作网页交互特效--JavaScript操作BOM对象

    JavaScript操作BOM 一.window对象: 二.window对象的属性和方法 1.windows对象的常用属性: 语法:window.属性名="属性值" 2.windo ...

  8. mysql kill所有Sleep/Execute进程

    现查出需要kill的进程: SELECT GROUP_CONCAT(CONCAT('kill ',id) SEPARATOR '; ') AS cmd FROM information_schema. ...

  9. 5-STM32物联网开发WIFI(ESP8266)+GPRS(Air202)系统方案升级篇(,远程升级GPRS内部程序)

    https://www.cnblogs.com/yangfengwu/p/10410202.html 与升级WIFI相同介绍的不再叙述  先看WIFI升级的: ↑ 演示视频: https://www. ...

  10. package.json文件说明解释

    1.package.json是什么? 什么是Node.js的模块(Module)?在Node.js中,模块是一个库或框架,也是一个Node.js项目.Node.js项目遵循模块化的架构,当我们创建了一 ...