Leetcode Basics 30 days array 30 Series title: simulation method

Array: Add a
stem that:
given a non-negative integer non-empty array of integers represented, on the basis of the number plus one.
Most significant digit stored in the first array, each element of the array stores a number only.
You may assume that in addition to the integer 0, the integer does not begin with a zero.
Reference Example:
Example 1:
Input: [1,2,3]
Output: [2,4]
Explanation: numeral 123 denotes the input array.
Example 2:
Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: input array represents a number 4321.
This question is the basis for an array of problems, its essence is a simulation problem.
This question has a certain sense of engineering applications, as we all know, the computer's floating-point arithmetic error, investigate the details of a considerable number of developers often superficial understanding. Take a look:
make friends all know that the development of a computer program in the process, is converted into a binary number to a decimal number for computing. For this transformation, which is to reverse modulo 2 addition method, for example:
the decimal number 13 is converted into a binary way
Leetcode Basics 30 days array 30 Series title: simulation method
transforming into a mathematical expression is (13) ₁₀ = (1101) ₂
to decimal to binary conversion using fractional method 2 is to take forward the rounding method, such as decimal 0.75
, ie (0.75) ₁₀ = (0.11) ₂
Leetcode Basics 30 days array 30 Series title: simulation method
lower if we consider the 0.6 decimal to binary conversion of it? If you still use-by-two to take forward what the whole process will happen?
Leetcode Basics 30 days array 30 Series title: simulation method
If Mathematically, (0.75) ₁₀ = (0.1001 ) ₂
Why is there this cycle represent it? Because there is evidence on mathematics, decimal notation and binary notation, only integer in the range have a clear correspondence relationship, and the fractional part is not. Thus, in binary arithmetic, the computer numerical solver is defective.
Aware of this, we would ask that we are not related to the operation and the decimal point are not reliable it? Obviously not, the most common is our financial activities. Almost all data and related transactions, that is a number that can not be wrong, then the calculation process behind them is how to ensure it?
The answer is simulated.
Primary Mathematics textbooks taught in such a case: 123.4 + 5.67, we look at, and the median addend addend is not the same, a primary school teacher in teaching will emphasize alignment decimal point, the process is as follows:
Leetcode Basics 30 days array 30 Series title: simulation method
this computer in order to have a term is called. Is the so-called binary order, the order is 10 decimal, binary order is 2, the specific mathematical expression is
123.4. 1 = 2 × × 10¹ 10² + + + 100. 3 × 10¹. 4 ×
5.67 × 100 =. 5. 6 × + 10-¹ + 7 × 10- ²
we know integer arithmetic computer is not defective, if we do, is not it can achieve the purpose of solving the accurate?
123.4 + 5.67 = (12340 + 567 ) × 10² = 12907 × 10² = 129.07
which is commonly used to calculate the accuracy of the calculation of thought, which is why do the trading system at the core of the Java language, the amount is usually not float, double and decimal types with reason.
Understand this knowledge, we know this seemingly common problem, in fact, it is a developer of enterprise software with the real problem. So, in reality the calculations, it is how to deal with the above order, the computation process it? The answer is an array. We use an array to simulate every one, and then use the methods taught elementary school mathematics textbooks, hand-calculate it.
Back to the subject following: function signature subject is such that: vector <int> plusOne (vector <int> & digits)
incoming parameter is digits, the vector is an integer, the subscript number of bits are arranged in ascending order, that is the subscript 0 corresponds to the highest digit, the last digit represents the lowest number of such reference samples: [1,2,3], he said that decimal 123, the subscript 0 corresponds to one hundred 1, the subscript 2 corresponds to the bits of decimal computation process 3. analog is every 10 carry, this question we first find the lowest position, if not the lowest bit 9, taken directly adding the subscript values corresponding to 1, If the number is 9, then, it is less straightforward to 0, and details on an angle corresponding to the target number plus 1. Note that, if this number is exactly [9,9,9] then return should be increased to obtain a value [1,0,0,0]. In order to facilitate the programming, we start from the last one. Code is as follows:
. 1 Vector <int> plusOne (Vector <int> & digits) {
2 int = n-digits.size ();
. 3 for (int. 1-n-= I; I> = 0; I -) {
. 4 IF (digits [I] ==. 9)
. 5 digits [I] = 0;
. 6 the else {
. 7 digits [I] ++;
. 8 digits return;
. 9}
10}
. 11 digits [0] =. 1;
12 is digits.push_back (0 );
13 is return digits;
14}
The second row, first obtains the length of the current vector of digits, and copy it to n, and the subscript n-1 from the start, a reverse traverse the entire array, because i is n-1 from the beginning, we first determined whether the current position is 9 , if it is 9, then put the current position is set to 0, and traversing forwardly to a front position as long as the former is not a position 9, then put the position at this time is incremented by 1, and then returns. If all loop end did not return, then that must be passed in 999 of this type. Then after a cycle, first subscript 0 to 1, and then add a 0, to complete all the calculation process.
Leetcode Basics 30 days array 30 Series title: simulation method
Course Link: https://edu.51cto.com/course/18440.html
Leetcode Basics 30 days array 30 Series title: simulation method

Reproduced in: https: //blog.51cto.com/xiacaojun/2410616

Guess you like

Origin blog.csdn.net/weixin_33813128/article/details/92879156