Leetcode7~9 Reverse integer/string to Integer (atoi)/palindrome number

Source: Internet
Author: User

One: Reverse Integer

Topic:

Reverse digits of an integer.

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

Links: https://leetcode.com/problems/reverse-integer/

Analysis: This problem by constantly taking the remainder in a vector, and then multiplied by the corresponding 10^i to add, here is the main consideration whether overflow, so the result is set to a long long int, When the maximum value greater than int is 0x7fffffff or less than the minimum value of int 0x800000000, 0 is returned, otherwise result is returned.

Code:

#define MAXINT (int) 0x7fffffff       //int maximum value ==2147483647     unsigned int maximum is 4294967295#define minint (int) 0x80000000      //int Minimum value 2147483648class solution {public:    int reverse (int x) {        vector<int> v;        while (x! = 0) {            int a = x%10;            V.push_back (a);            x = X/10;        }        Long long int result = 0;                for (int i = 0; i < v.size (); i++) {            result + = V[i]*pow (10.0,v.size ()-i-1);        }        if (Result > maxInt | | result < MININT) return 0;     Solve overflow problem by long long int        return result;}            };

two: String to Integer (atoi)

Title:Implement atoi to Convert a string to an integer.

Links: https://leetcode.com/problems/string-to-integer-atoi/

Analysis: This problem is not difficult, the key is to meet the test sample more abnormal, such as "123456ab78", but also to return 123456 and so on

Code:

#define MAX (int) 0x7fffffff#define Min (int) 0x80000000class Solution {public:int atoi (string str) {if (str.size        () <= 0) return 0;         for (int i = 0; i < str.size (); i++) {//Remove the preceding space if (str[i]! = ") {str = STR.SUBSTR (i); break;}            } int c = 1;int len = str.size (); if (IsDigit (Str[0])) {//If the first digit represents an integer then precede with a + sign str = "+" + str;        len = Len +1;        } if (str[0] = = ' + ') c = 1;        else if (str[0] = = '-') c =-1; else return 0;for (int i = 1; i < Len; i++) {//Remove all characters from the beginning of the letter if (!isdigit (Str[i]) | | I >= 12) {//over 12 bits) or it may be super A long long range str = str.substr (0, I); break;}}                       Len = Str.size ();                Long long int result = 0;;            for (int i = 1; i < Len; i++) {if (!isdigit (Str[i])) return 0;            StringStream SS;            int temp;            SS << Str[i];            SS >> Temp;        Result + = temp * POW (10.0, len-i-1.0) *c; } if (rEsult > Max) return max;if (Result < min) return Min;    return result; }};
Three: palindrome number

Topic:

Determine whether an integer is a palindrome. Do this without extra space.

Click to show spoilers.

Some hints:

Could negative integers be palindromes? (ie,-1)

If you is thinking of converting the integer to string, note the restriction of using extra space.

You could also try reversing an integer. However, if you had solved the problem "Reverse integer", you know that the reversed integer might overflow. How do would handle such case?

There is a more generic the solving this problem.

Link:

https://leetcode.com/problems/palindrome-number/

Analysis: This topic has a few limitations: 1 can not be used extra space, so can not be converted to a string to find Palindrome 2: First reverse integer, then overflow how to do? So the problem is to find the number of digits of int, and then remove the lowest and highest bits to compare.

Class Solution {public:    bool Ispalindrome (int x) {        if (x < 0) return false;        int d = 0;        int y = x;        while (Y! = 0) {    //the number of digits of X is first calculated            y = y/10;            d++;        }        while (x! = 0) {            int lowbit = x Ten;   Find the lowest bit of x            int highbit = X/POW (10.0, d-1.0);   Find the highest bit of x            if (lowbit! = Highbit) return false;            x = (X-lowbit-highbit * POW (10.0, d-1.0))/10;            d = d-2;        }        return true;                    }};



Leetcode7~9 Reverse integer/string to Integer (atoi)/palindrome 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.