Leetcode: reverse_integer

Source: Internet
Author: User

I. Reverse the integer of the question.

Example 1: x = 123, 321 is returned.

Example 2: x =-123,-321 returns

Note:

1. What is output if the last digit of an integer is 0? That is, for example, 10,100.

2. Assume that the input is a 32-bit integer, then the anti-transfer 1000000003 overflows.

Ii. Analysis

In fact, I initially thought that ITOA () and atoi () can be done directly, but later I found that it could not be done (maybe I did not expect how to do it)

Later, we found that we only need to reconstruct this number, that is, every time we get the last bit of X, convert it to the high position of the number we are looking for, and correspond in sequence to obtain a solution.


Class solution {public: int reverse (int x) {int flag = 0; while (x) {flag = flag * 10 + x % 10; X/= 10 ;} return flag ;}}


Leetcode: reverse_integer

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.