Determines whether an integer is a return number.

Source: Internet
Author: User
Problem

Determines whether a positive integer is a return number. For example, 12121 is the number of replies, and 1231 is not the number of replies.

Solution 1: Convert to string and then judge

To determine whether an integer is a return number, the most natural idea is to convert the integer into a string and then judge based on the symmetric characteristics of the return. To convert a number to a string, you can use the ITOA function to determine whether the string is a return string. The Code is as follows:

bool isPalindrome(string &str){    int begin = 0, end = str.length()-1;    while (begin < end) {        if (str[begin] == str[end]) {            begin++;            end--;        } else {            return false;        }    }    return true;}

Solution 2: digital flip

Because it is an integer, you can find the flipped value of this integer to see if it is equal to the original integer. If they are equal, they are the number of replies. Otherwise, they are not. The code for turning an integer is as follows. The returned value is the converted integer. For example, if 12321 is flipped to 12321, it is the number of input files. If 1231 is flipped to 1321, It is not equal to 1231, so it is not the number of input files.

int reverse(int num) {  assert(num >= 0);   int rev = 0;  while (num != 0) {    rev = rev * 10 + num % 10;    num /= 10;  }  return rev;}

However, there is a potential problem here, that is, the integer after the flip may overflow. Of course, we can use the long type to save the flip result. But this solution is not perfect in general. We need to find a more general solution.

Solution 3: digit embedding

We can find a more general solution, that is, compare the first 1st bits of the integer with the last 1 bits to see if they are equal. If they are not equal, false is directly returned. If they are equal, the next step is to determine the remaining position, just like the process of determining the input string. The Code is as follows:

bool isPalindrome(int x) {  if (x < 0) return false;  int div = 1;  while (x / div >= 10) {    div *= 10;  }  while (x != 0) {    int l = x / div;      int r = x % 10;    if (l != r) return false;    x = (x % div) / 10;    div /= 100;  }  return true;}

For example, if the integer is 121, the DIV is initially set to 100. Therefore, L = 21/100 = 1 is the 1st bits of the integer, and r = 121% 10 = 1 is the last 1 bits. If the two digits are equal, the loop continues. If the value of X is set to 2, The DIV is divided by 2nd to 1. The reason why the DIV is divided by 100 is that two bits are compared each time.

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.