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.

一个非负数字存在一个数组里,高位在前面,给这个数加一。

解法1:从数组的最后一位开始加1,如果是不是9,就直接加1后返回目前的数就可以了。如果是9,最后一位加1后为0进位为1,下一个数也是9加1后为0进位为1,如果在加的过程中有哪一位不是9,那么就加1后返回当前数。如果一直到最高位都是9,当前位变成了0,然后在前面多加个1。由于数组只能在后面添加数字,所以可以把第一个数变为1,最后加一个0;也可以在建一个数组长度比原来多1的数组,第一个元素为1, 其它元素为0。

解法2:由于数组添加一位数字要在最后,为了方便操作先把数字逆序,然后用取余、取模操作来算当前位的值和进位,最后在逆序输出返回。

Java:

public int[] plusOne(int[] digits) {

    int n = digits.length;
for(int i=n-1; i>=0; i--) {
if(digits[i] < 9) {
digits[i]++;
return digits;
} digits[i] = 0;
} int[] newNumber = new int [n+1];
newNumber[0] = 1; return newNumber;
}  

Java:

public class Solution {
public int[] plusOne(int[] digits) {
int n = digits.length;
for (int i = digits.length - 1; i >= 0; --i) {
if (digits[i] < 9) {
++digits[i];
return digits;
}
digits[i] = 0;
}
int[] res = new int[n + 1];
res[0] = 1;
return res;
}
}

Java:

class Solution {
public int[] plusOne(int[] digits) {
if (digits.length == 0) return digits;
int carry = 1;
for (int i = digits.length - 1; i >= 0; --i) {
if (carry == 0) return digits;
int sum = digits[i] + carry;
digits[i] = sum % 10;
carry = sum / 10;
} if (carry == 0) return digits; int[] res = new int[digits.length + 1];
res[0] = 1;
for (int i=1, i < res.length; i++) {
rest[i] = digits[i - 1];
} return res;
}
} 

Python:

# in-place solution
class Solution(object):
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
for i in reversed(xrange(len(digits))):
if digits[i] == 9:
digits[i] = 0
else:
digits[i] += 1
return digits
digits[0] = 1
digits.append(0)
return digits

Python:

# Time:  O(n)
# Space: O(n)
class Solution2(object):
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
result = digits[::-1]
carry = 1
for i in xrange(len(result)):
result[i] += carry
carry, result[i] = divmod(result[i], 10)
if carry:
result.append(carry)
return result[::-1]   

C++: In place, Time: O(n), Space: O(1)

class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
for (int i = digits.size() - 1; i >= 0; --i) {
if (digits[i] == 9) {
digits[i] = 0;
} else {
++digits[i];
return digits;
}
}
digits[0] = 1;
digits.emplace_back(0);
return digits;
}
}; 

C++:  Time: O(n), Space: O(n)

class Solution2 {
public:
vector<int> plusOne(vector<int>& digits) {
vector<int> result(digits.rbegin(), digits.rend());
int carry = 1;
for (auto& num : result) {
num += carry;
carry = num / 10;
num %= 10;
}
if (carry == 1) {
result.emplace_back(carry);
}
reverse(result.begin(), result.end());
return result;
}
};

  

类似题目:
[LeetCode] 67. Add Binary 二进制数相加

[LeetCode] 369. Plus One Linked List 链表加一运算

All LeetCode Questions List 题目汇总

  

[LeetCode] 66. Plus One 加一的更多相关文章

  1. [leetcode]66. Plus One加一

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

  2. 前端与算法 leetcode 66. 加一

    目录 # 前端与算法 leetcode 66. 加一 题目描述 概要 提示 解析 解法一 解法二 算法 # 前端与算法 leetcode 66. 加一 题目描述 给定一个由整数组成的非空数组所表示的非 ...

  3. 每日一道 LeetCode (14):数组加一

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  4. LeetCode 66. Plus One(加1)

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

  5. Java实现 LeetCode 66 加一

    66. 加一 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示 ...

  6. [LeetCode]66. 加一(数组)

    ###题目 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 ...

  7. python(leetcode)-66加一问题

    给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 1: 输入 ...

  8. LeetCode(66): 加一

    Easy! 题目描述: 给定一个非负整数组成的非空数组,在该数的基础上加一,返回一个新的数组. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整数不会 ...

  9. Leetcode——66.加一

    @author: ZZQ @software: PyCharm @file: leetcode66_加一.py @time: 2018/11/29 16:07 要求:给定一个由整数组成的非空数组所表示 ...

随机推荐

  1. Codeforces I. Vessels(跳转标记)

    题目描述: Vessels time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  2. Java.io.tmpdir介绍

    System.getproperty(“java.io.tmpdir”)是获取操作系统缓存的临时目录,不同操作系统的缓存临时目录不一样, 在Windows的缓存目录为:C:\Users\登录用户~1\ ...

  3. 分析和研究Monkey Log文件

    Log 在Android中的地位非常重要,要是作为一个android程序员不能过分析log这关,算是android没有入门吧 . 下面我们就来说说如何处理log文件 . 什么时候会有Log文件的产生 ...

  4. test20190924 老L

    80+50+100=230.T1没做出来说明我数列学得不好? LOLO 的含树 现有函数 \[ g_m(i)=\begin{cases} 0, & 0 \leq i \leq m\\ i-1+ ...

  5. oracle 年龄分档,不用case when 的方法

    一般我们出分档数据都是case when ,但是如果是对年龄等一些字段进行细分,比如五岁一档,我们如果用case when就会特别麻烦,写的特别多,这里我介绍一种简单的方法,对细分的字段进行处理: 建 ...

  6. apache commons-configuration包读取配置文件

    1.pom依赖添加 <!-- 配置文件读取 --> <dependency> <groupId>commons-configuration</groupId& ...

  7. 关于原生js的节点兼容性

    关于节点的兼容性: 1:获取元素的子节点 a: childNodes:获取元素的子节点,空文本,非空文本,注释,获取的比较全面, 如果只是想获取元素的子节点,请用(children) b:     c ...

  8. learning scala zipAll

    If two Iterables aren't the same size, then zipAll can provide fillers for what it couldn't find a c ...

  9. 9-ESP8266 SDK开发基础入门篇--编写串口上位机软件

    https://www.cnblogs.com/yangfengwu/p/11087613.html 页面修改成这样子             现在看串口发送数据 点击点亮 发送0xaa 0x55 0 ...

  10. 1-移远GSM/GPRS M26 模块 Mini板 开发板(使用说明)

    板子预览 引脚说明 供电 关于串口电压匹配引脚: 上面一版朋友测试反应的问题 (上面的内容不删除,因为已经出售了1套) 1,源码开发完以后,烧录完成 PWRKEY按键不能使用了,需要断电上电,那么就需 ...