Leetcode No.66 plus one (c + + implementation)

Time:2022-1-3

1. Title

1.1 English title

Given a non-empty array of decimal digits representing a non-negative integer, increment 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 contains a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.

1.2 Chinese title

Given a non negative integer represented by a non empty array of integers, add one to the number.

The highest digit is stored in the first place of the array, and each element in the array stores only a single digit.

You can assume that this integer does not start with zero except the integer 0.

1.3 input and output

input output
digits = [1,2,3] [1,2,4]
digits = [4,3,2,1] [4,3,2,2]
digits = [0] [1]

1.4 constraints

  • 1 <= digits.length <= 100
  • 0 <= digits[i] <= 9

2. Experimental platform

IDE:VS2019
IDE version: 16.10.1
Language: C + + 11

3. Procedure

3.1 test procedure

#include "Solution.h"
#include 	// std::vector
#include	// std::cout
using namespace std;

//Main program
void main()
{
	//Input
	vector digits = { 9,9,9 };

	Solution solution; //  Instantiate solution
	vector output = solution. plusOne(digits); //  Main function

	//Output
	for (auto i : output)
		cout << i;
}

3.2 function program

3.2.1 optimal algorithm

(1) Code

#pragma once
#include    // std::vector
//#include  // INT_ Min integer minimum
#include // std::max
using namespace std;

//Main function
class Solution {
public:
    vector plusOne(vector& digits) 
    {
        int carry = 1; //  Store carry digit
        int length = digits.size(); 
        for (int i = length - 1; i >= 0; --i) 
        {
            //If a bit does not have the carry of the previous bit, there will be no carry in the high bit, that is, the high bit remains unchanged and the result can be returned directly
            if (carry == 0) return digits; 

            //If there is a carry, the carry value and its current digit need to be calculated, that is, a cycle needs to be carried out
            int sum = digits[i] + carry;
            digits[i] = sum % 10;
            carry = sum / 10;
        }

        //If carry is required for the highest bit, insert "1" in the highest bit
        if (carry == 1) digits.insert(digits.begin(), 1);
        return digits;
    }
};

reference resources:https://www.cnblogs.com/grandyang/p/4079357.html

(2) Interpretation

reference resources:
https://blog.csdn.net/linhuanmars/article/details/22365957
https://blog.csdn.net/Tianc666/article/details/105769411

3.2.2 visual solution method

(1) Code 1

#pragma once
#include    // std::vector
//#include  // INT_ Min integer minimum
#include // std::max
using namespace std;

//Main function
class Solution {
public:
    vector plusOne(vector& digits) {
        vector digitsNew(digits.size() + 1);// New number (reverse order)
        int count = 1;// Carry (0 / 1)
        for (int i = 0; i < digits.size(); i++)
        {
            int tempOrder = digits.size() - 1 - i;
            int sumTemp = digits[tempOrder] + count;
            count = sumTemp / 10;
            digitsNew[i] = sumTemp % 10;+
            if (i == digits.size() - 1)
                digitsNew[i + 1] = count;
        }

        //Reverse order to positive order
        int length = digits.size() + digitsNew.back();
        vector digitsFinal(length);
        for (int i = length - 1; i >= 0; i--)
            digitsFinal[i] = digitsNew[length - 1 - i];
        return digitsFinal;
    }
};

(2) Train of thought

First reverse the evaluation, and then reverse it. But my code is not very concise. You can refer to code 2

(3) Code 2

#pragma once
#include    // std::vector
//#include  // INT_ Min integer minimum
#include // std::max
using namespace std;

//Main function
class Solution 
{
public:
    vector plusOne(vector &digits) 
    {
        vector ret(digits);
        reverse(ret.begin(), ret.end());

        int flag = 1;
        for(int i = 0; i < ret.size(); i++)
        {
            ret[i] += flag;
            //The result of the flag here is either 0 or 1
            flag = ret[i] / 10;
            ret[i] %= 10; 
        }

        if (flag == 1)
            ret.push_back(1);

        reverse(ret.begin(), ret.end());

        return ret;
    }
};

reference resources:https://theonegis.blog.csdn.net/article/details/44258329

3.2.3 other algorithms

(1) Code

#pragma once
#include    // std::vector
//#include  // INT_ Min integer minimum
#include // std::max
using namespace std;

//Main function
class Solution {
public:
    vector plusOne(vector &digits) {
        int n = digits.size();
        for (int i = n - 1; i >= 0; --i) {
            if (digits[i] == 9) digits[i] = 0;
            else {
                digits[i] += 1;
                return digits;
            }
        }
        if (digits.front() == 0) digits.insert(digits.begin(), 1);
        return digits;
    }
};

reference resources:https://www.cnblogs.com/grandyang/p/4079357.html

(2) Interpretation

Save the number on each bit of a number into a one-dimensional vector. The highest bit is at the beginning. We need to add one to this number, that is, add one to the end. If the end number is 9, there will be a carry problem. If the number on the previous bit is still 9, we need to carry forward. The specific algorithm is as follows: first judge whether the last bit is 9. If not, add one directly to return. If so, assign 0 to this bit, and then continue to check the previous bit. In the same way, you know that the first bit is checked. If the first bit was originally 9 and a new bit will be generated after adding one, the last thing to do is to check whether the first bit after the operation is 0. If so, add a 1 at the beginning.
reference resources:https://www.cnblogs.com/grandyang/p/4079357.html

Recommended Today

Delivery without leaving home – exclusive delivery of secret script (the second time)

Authors: Chen Nian, Congyan, Xin Yu Hello, I’m wang Xiaochui, who focuses on software application delivery! Our delivery iron triangle team (focus on delivery me, development brother tiezi, pre-sales boss Qiang brother) is here again. As an ISV enterprise providing big data analysis services, in the face of the still severe domestic epidemic situation and […]