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. 通过Python实现mysql查询数据库实例

    #coding:utf-8 ''' Created on 2017年10月25日 @author: li.liu ''' import pymysql db=pymysql.connect('loca ...

  2. vue 弹框

    弹框展示: 代码: <template> <div> <el-col :span="9" style="text-align: right; ...

  3. CodeForces - 83D:Numbers (数学&递归 - min25筛 )

    pro:给定三个整数L,R,P求[L,R]区间的整数有多少个是以P为最小因子的.L,R,P<2e9; sol: 一: 比较快的做法是,用函数的思想递归. 用solve(N,P)表示求1到N有多少 ...

  4. SSH——ssh_exchange_identification: read: Connection reset by peer

    前言 ssh远程连接出错 步骤 查看ssh的详细信息 [root@pre-nginx02 ~]# ssh -v 192.168.1.164 OpenSSH_6.6.1, OpenSSL 1.0.1e- ...

  5. gitlab修改ip

    gitlab 修改ip的两种方式: 修改/etc/gitlab/gitlab.rd 里面的#external_url 'http://gitlab.example.com' 为ip地址,然后重新构建- ...

  6. Optimal Marks SPOJ - OPTM(最小割)

    传送门 论文<最小割模型在信息学竞赛中的应用>原题 二进制不同位上互不影响,那么就按位跑网络流 每一位上,确定的点值为1的与S连一条容量为INF的有向边.为0的与T连一条容量为INF的有向 ...

  7. mybatis-generator 插件

    首先肯定要有mybatis的依赖 <!--mybatis spring--> <dependency> <groupId>org.mybatis.spring.bo ...

  8. CSS行内元素

    一.典型代表 span a ,strong em del, ins 二.特点: 在一行上显示 不能直接设置宽高 元素的宽和高就是内容撑开的宽高. <style type="text/c ...

  9. ubuntu编译PCRE时出现 line 81: aclocal-1.14: command not found错误

    WARNING: 'aclocal-1.14' is missing on your system. You should only need it if you modified 'acinclud ...

  10. GDOI2018 小学生图论题 [NTT]

    并没有传送门qwq 思路 首先要知道一个结论(或者说是一个套路):一个竞赛图缩点之后必定是一条链. 那么强联通分量的个数,就是这条链的边数+1. 考虑一条边什么时候会出现:当且仅当点集可以被分成\(S ...