[LeetCode-interview algorithm classic-Java implementation] [007-Reverse Integer (Flip Integer)], leetcode -- java
[007-Reverse Integer (Flip Integer )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Original question
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x =-123, return-321
Theme
Enter an integer to flip it.
Solutions
Calculate the commercial law by finding the remainder.
Code Implementation
Public class Solution {public int reverse (int x) {long tmp = x; // prevent result overflow long result = 0; while (tmp! = 0) {result = result * 10 + tmp % 10; tmp = tmp/10;} // determine if (result <Integer. MIN_VALUE | result> Integer. MAX_VALUE) {result = 0;} return (int) result ;}}
Evaluation Result
Note
Please refer to the following link for more information: http://blog.csdn.net/derrantcm/article/details/46938355]
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.