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 {
public:
    vector plusOne(vector& digits) {
        vector res = digits;
        res.insert(res.begin(), 0);
        int sz = res.size();
        int carry = 1;
        for(int i = sz-1; i >= 0; i--){
            res[i] += carry;
            if(res[i] >= 10){
                carry = 1;
                res[i] -= 10;
            }else{
                carry = 0;
            }
        }
        if(res[0] == 0){
            res.erase(res.begin());
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/zhanzq/p/10556062.html

时间: 2024-08-29 19:31:12

leetcode 66. 加一(Plus One)的相关文章

[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. 加一(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