Reverse integer rotation number

Source: Internet
Author: User

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x =-123, return-321

Local attention to positive and negative numbers is critical, and the Implementation part may not be optimal, according to your own ideas:

Set ret = 1; perform the remainder mod on X each time, and then ret = RET * 10 + MOD; (First let ret = mod), and finally return ret

The Code is as follows:

 1 public class Solution { 2     public int reverse(int x) { 3         int result = 0; 4         int flag = (x < 0) ? 1 : 0; 5          6         x = (x < 0) ? -x : x; 7          8         int first = 1; 9         while(x!=0){10             int mod = x%10;11             if(first == 1){12                 result = mod;13                 first = 0;14             }else{15                 result *= 10;16                 result += mod;17             }18             19             x /= 10;20             21         }22         23         return (flag == 1) ? -result : result;24         25     }26 }

 

Reverse integer rotation number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.