Digital inversion problem (Java implementation)
One problem encountered in the interview recorded.
Problem Scenario:
Enter a series of numbers, after the reverse order output.
Example:
Input: 123, Output: -321
Input: 100, output: 1
Program:
import java.util.Scanner;
public class NumberReversal {
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
long time1=System.currentTimeMillis();
int result=0;
int tmp=Math.abs(n);
while(tmp>0){
result*=10;
result+=tmp%10;
tmp/=10;
}
System.out.println(n>=0?result:-result);
long time2=System.currentTimeMillis();
System.out.println(time2-time1);
}
}
Test shots:
We can see the efficiency of the algorithm is still very high.
Life has a limit but knowledge not.