LeetCode(66): 加一

Easy!

题目描述:

给定一个非负整数组成的非空数组,在该数的基础上加一,返回一个新的数组。

最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。

你可以假设除了整数 0 之外,这个整数不会以零开头。

示例 1:

输入: [1,2,3]
输出: [1,2,4]
解释: 输入数组表示数字 123。

示例 2:

输入: [4,3,2,1]
输出: [4,3,2,2]
解释: 输入数组表示数字 4321。

解题思路:

将一个数字的每个位上的数字分别存到一个一维向量中,最高位在最开头,我们需要给这个数字加一,即末尾数字加一,如果末尾数字是9,那么则会有进位问题,而如果前面位上的数字本身也为9,则需要继续向前进位。

具体算法如下:首先判断最后一位是否为9,若不是,直接加一返回,若是,则该位赋0,再继续查前一位,同样的方法,直到查至第一位结束。如果第一位原本为9,加一后会产生新的一位,那么最后要做的是,查运算完的第一位是否为0,如果是,则在最前头加一个1。

C++解法一:

 1 class Solution {
 2 public:
 3     vector<int> plusOne(vector<int> &digits) {
 4         int n = digits.size();
 5         for (int i = n - 1; i >= 0; --i) {
 6             if (digits[i] == 9) digits[i] = 0;
 7             else {
 8                 digits[i] += 1;
 9                 return digits;
10             }
11         }
12         if (digits.front() == 0) digits.insert(digits.begin(), 1);
13         return digits;
14     }
15 };

我们也可以使用跟Add Binary(http://www.cnblogs.com/grandyang/p/4084971.html)类似的做法,我们将carry初始化为1,然后相当于digits加了一个0,处理方法跟之前那道题一样。

C++解法二:

 1 class Solution {
 2 public:
 3     vector<int> plusOne(vector<int>& digits) {
 4         if (digits.empty()) return digits;
 5         int carry = 1, n = digits.size();
 6         for (int i = n - 1; i >= 0; --i) {
 7             if (carry == 0) return digits;
 8             int sum = digits[i] + carry;
 9             digits[i] = sum % 10;
10             carry = sum / 10;
11         }
12         if (carry == 1) digits.insert(digits.begin(), 1);
13         return digits;
14     }
15 };

原文地址:https://www.cnblogs.com/ariel-dreamland/p/9151103.html

时间: 2024-07-29 08:37:07

LeetCode(66): 加一的相关文章

[leetcode] 66. 加一

66. 加一 模拟加法运算,很简单 注意进位即可 class Solution { public int[] plusOne(int[] digits) { int k = digits.length - 1; digits[k] += 1; while (k > 0) { if (digits[k] < 10) break; digits[k] -= 10; digits[k - 1] += 1; k--; } if (digits[0] >= 10) { digits[0] -= 1

LeetCode 66. 加1

题目: 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 1: 输入: [1,2,3] 输出: [1,2,4] 解释: 输入数组表示数字 123. 示例2: 输入: [4,3,2,1] 输出: [4,3,2,2] 解释: 输入数组表示数字 4321. 思路与解答: 题解一: 这道题最直观的方法就是首先判断末位是否为9,不为9,直接加1返回,具体为: 从末位开

leetcode 66. 加一(Plus One)

目录 题目描述: 示例 1: 示例 2: 解法: 题目描述: 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 1: 输入: [1,2,3] 输出: [1,2,4] 解释: 输入数组表示数字 123. 示例 2: 输入: [4,3,2,1] 输出: [4,3,2,2] 解释: 输入数组表示数字 4321. 解法: class Solution { publi

LeetCode 66. 加一(java)

题目: https://leetcode-cn.com/problems/plus-one/ 如果digits数组最后一位小于9,则只需要将digits数组最后一个数+1,返回digits数组即可:如果最后一位等于9,则需要设置一个循环进行加法模拟,即对每一位进行判断,小于10退出循环,如果等于10,进位,这种情况则需创建一个新数组,长度为digits长度加一,将数据保存进去即可. 代码: class Solution { public int[] plusOne(int[] digits) {

Leetcode#66.加一 (C++解法)

目录 一.题目描述 二.解法 9,9,9,9 1,2,3,4 & 1,9,9,9 题目链接: https://leetcode-cn.com/problems/plus-one/ 一.题目描述 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 1: 输入: [1,2,3] 输出: [1,2,4] 解释: 输入数组表示数字 123. 示例 2: 输入: [4,

LeetCode 66. Plus One(加1)

Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. You may assume the integer do not contain any leading zero, except the number 0 itself. The digits are stored such that the most significant digit is at

LeetCode 66 Plus One(加一)(vector)

翻译 给定一个以一系列数字表示的非负数.将其加一并转换成数字. 数字存储的最高位在列的最前面. 原文 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. 分析 一看到这个题目我就想起来前面做过的一道关于求

[leetcode]66. Plus One加一

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 assum

leetCode 66. Plus One 数组

66. 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,求得到的新数组. 高位在前. class Solution { p

leetcode—66 Plus One(数组中的值+1进位操作,数组扩充)

Plus One 66  Total Accepted: 48227 Total Submissions: 157869 My Submissions Question Solution 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