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,3,2,1]
输出: [4,3,2,2]
解释: 输入数组表示数字 4321。

二、解法

graph LR
A[加1有多少种情况]
A --> B[1 2 3 4 末尾加1,无进位]
A -->C[1 9 9 9存在进位,但结果还是四位]
A -->d[9 9 9 9存在进位,并且进位会导致位数加1]

9,9,9,9

可以想到,加1之后进位,并产生新的数字的情况只有每一位都是9

所以这个可以作为一种特殊情况单独写。

1,2,3,4 & 1,9,9,9

解决第一种特殊情况之后,我们可以保证其他情况都不会产生新的位(原来是几位就是几位)。

设立一个虚拟的指针p从后往前遍历。

digit[p] + 1 只会小于等与1

如果digit[p]+1 == 10 , 那么让digits[p] = 0,并让p -- ,并让前一位做相同的操作。

如果digit[p]+1 != 10 , 那么让digits[p] = digits[p]+1,此时可以结束循环

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int len = digits.size();

        // 9..9
        //遍历一遍digits
        //如果存在不等于9的位,flag = 0
        //否则flag再循环结束的时候会保持1不变
        int flag = 1;
        for(int i = 0 ; i < len ; i++)
            if(digits[i] != 9){
                flag = 0;
                break;
            }
        if(flag){	//确认是9..9的格式
            digits[0] = 1;
            for(int i = 1; i < len ;i++)
                digits[i] = 0;
            digits.push_back(0);
            return digits;
        }

        //1234 && 1999
        //设立p指针从最后以为向 ← 遍历
        //temp用来保存digits[p]+1
        //再次用到flag变量,如果在计算过程中没有进位(temp!=10),则可以提前结束循环
        int p = len - 1;
        int temp;
        flag = 1;
        while(flag && p >= 0){
            temp = digits[p] + 1;
            digits[p] = temp%10;
            if(temp != 10) flag = 0;
            p--;
        }

        return digits;
    }
};

原文地址:https://www.cnblogs.com/Justdocument/p/12596336.html

时间: 2024-07-29 08:36:51

Leetcode#66.加一 (C++解法)的相关文章

[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]Linked List Cycle II解法学习

问题描述如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 从问题来看,如果可以充分利用额外空间的话,这个题目是不难的,然而题目提出了一个要求,能否在不使用任何额外空间的情况下解决这个问题. 通过反复思考,我觉得这题类似于追击问题,可以用一个

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