1. Title
1.1 English title
You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.
Merge nums1 and nums2 into a single array sorted in non-decreasing order.
The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n.
1.2 Chinese title
Given two sorted integer arrays nums1 and nums2, nums2 is combined into a sorted array nums1.
The number of initialized elements in nums1 and nums2 are m and N, respectively. Suppose nums1 has enough space (size greater than or equal to m + n) to accommodate other elements in nums2.
1.3 input and output
input | output |
---|---|
nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 | [1,2,2,3,5,6] |
nums1 = [1], m = 1, nums2 = [], n = 0 | [1] |
nums1 = [0], m = 0, nums2 = [1], n = 1 | [1] |
1.4 constraints
- nums1.length == m + n
- nums2.length == n
- 0 <= m, n <= 200
- 1 <= m + n <= 200
- -109 <= nums1[i], nums2[j] <= 109
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;
}
};
#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;
}
#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