1. Overview
1.概述
In this tutorial, we’ll see how to reverse a number using a mathematical approach in Java. First, we’ll see what math operations are necessary for doing this, and then we’ll go through three different ways of implementing this.
在本教程中,我们将看到如何用Java中的数学方法来反转一个数字。首先,我们将看到这样做需要哪些数学运算,然后我们将通过三种不同的方式来实现这一目标。
2. Solution Approach
2.解决方案的方法
To start off, let’s take an example and see exactly what should happen. For example, we want the number 1234 to become 4321. This can be achieved with the following approach:
首先,让我们举个例子,看看到底应该发生什么。例如,我们希望数字1234变成4321。这可以通过以下方法来实现。
- get the last digit of the number
- we can apply modulus to get the last digit
- 1st iteration – 1234 % 10 = 4
- 2nd iteration – 123 % 10 = 3
- multiply the reversed number by 10 and add the digit found in the previous step
- 1st iteration – 0 * 10 + 4 = 4 (since there’s no reversed number to start with, we multiply with 0 in the 1st iteration)
- 2nd iteration – 4 * 10 + 3 = 43
- divide the original number by 10 and repeat from step 1 and continue until the number is not 0
- 1st iteration – 1234 / 10 = 123
- 2nd iteration – 123 / 10 = 12
3. Mathematical Implementation
3.数学上的实施
We want to translate the math operations above into code. This is possible in three different ways: using a while loop, a for loop, or recursion.
我们想把上面的数学运算转化为代码。这可以通过三种不同的方式实现:使用while循环、for循环或递归。
The approaches below also cater to negative values by using the absolute value of the number to be reversed and multiplying the reversed number by -1 if the original number is negative.
下面的方法也能满足负值的要求,即使用要反转的数字的绝对值,如果原数字为负数,则将反转的数字乘以-1。
3.1. while Loop
3.1.while 循环
The while loop is first on the list as it’s the clearest way of translating the math operations above:
while 循环是列表中的第一个,因为它是翻译上面的数学运算的最清晰的方式。
int reversedNumber = 0;
int numberToReverse = Math.abs(number);
while (numberToReverse > 0) {
int mod = numberToReverse % 10;
reversedNumber = reversedNumber * 10 + mod;
numberToReverse /= 10;
}
return number < 0 ? reversedNumber * -1 : reversedNumber;
3.2. for Loop
3.2.for循环
Using the for loop, the logic is the same as earlier. We skip the initialization statement of the for loop and use the number that is being reversed in the terminating condition:
使用for循环,其逻辑与前面一样。我们跳过for循环的初始化语句,在终止条件中使用被反转的数字。
int reversedNumber = 0;
int numberToReverse = Math.abs(number);
for (; numberToReverse > 0; numberToReverse /= 10) {
int mod = numberToReverse % 10;
reversedNumber = reversedNumber * 10 + mod;
}
return number < 0 ? reversedNumber * -1 : reversedNumber;
3.3. Recursion
3.3.递归
For recursion, we can use a wrapper method that calls the recursive method that returns the reversed number:
对于递归,我们可以使用一个包装方法,调用递归方法,返回反转的数字。
int reverseNumberRecWrapper(int number) {
int output = reverseNumberRec(Math.abs(number), 0);
return number < 0 ? output * -1 : output;
}
The recursion method implements the math operations in the same manner as the previous examples:
递归方法实现数学运算的方式与前面的例子相同。
int reverseNumberRec(int numberToReverse, int recursiveReversedNumber) {
if (numberToReverse > 0) {
int mod = numberToReverse % 10;
recursiveReversedNumber = recursiveReversedNumber * 10 + mod;
numberToReverse /= 10;
return reverseNumberRec(numberToReverse, recursiveReversedNumber);
}
return recursiveReversedNumber;
}
The recursive method returns the currently reversed number in each iteration and the number to be reversed is shortened with each iteration. This happens until the number to be reversed is 0, at which time we return the fully reversed number.
该递归方法在每次迭代中都会返回当前的反转数字,并且每次迭代都会缩短要反转的数字。这种情况一直持续到要反转的数字为0,这时我们会返回完全反转的数字。
4. Conclusion
4.总结
In this article, we explored three different implementations for reversing a number, using a while loop, a for loop, and recursion.
在这篇文章中,我们探讨了逆转数字的三种不同实现方式,使用了while循环、for循环和递归。
As always, the source code for the examples is available over on GitHub.
像往常一样,这些例子的源代码可以在GitHub上找到。