Leetcode Title Finishing-2 Reverse integer && String to Integer

Source: Internet
Author: User

Today's two questions about the basic data types of discussion, it is estimated to consider a variety of situations, to study carefully

7. Reverse Integer

Reverse digits of an integer.

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

Has a thought about this?

Here is some good questions to ask before coding. Bonus points for if you have already thought through this!

If the last digit are 0, what should the output being? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer and then the reverse of 1000000003 overflows. How should handle such cases?

For the purpose of this problem, assume a your function returns 0 when the reversed integer overflows.

Update (2014-11-10): Test cases had been added to test the overflow behavior.

Note: Reverses each bit in an integer, and the sign bit is reserved. There are some extreme situations to consider, and here are a few, such as, what is the last 0 rollover? Also, what if the expression range of the data type is exceeded after flipping? Other difficulties may also be encountered ^~^

Solution: You must define the maximum value as an int constant when considering the maximum value, otherwise the system assigns him a suitable type.

It is not possible to write this judgment directly.

if (temp >0x7fffffff | | Temp < 0x80000000)        {            return result;        }

Here's the code to commit:

Class Solution {public:    int reverse (int x) {            int result{0};            Long Long temp{0};            const int max_int=0x7fffffff;//0111 1111 1111 1111 ... 1111 32bit            const int min_int=0x80000000;//1000 0000 0000 0000 ... 0000 32bit    while (x! = 0)    {        temp = temp*10 + x%10;        x = X/10;        if (Temp > Max_int | | temp < min_int)        {            return result;        }    }    result = temp;    return result;}    ;

8. String to Integer (atoi)

Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please don't see below and ask yourself what is the possible input cases.

Notes: It is intended for the problem to be specified vaguely (ie, no given input specs). You is responsible to gather all the input requirements up front.

Update (2015-02-10): The signature of the C + + function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your co De definition.

Requirements for Atoi:

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, the starting from this character, takes a optional initial plus or minus sign followed by as many numerical digits as P Ossible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which is ignored and has no Effe CT on the behavior of this function.

If the first sequence of non-whitespace characters in STR isn't a valid integral number, or if no such sequence exists be Cause either str is empty or it contains only whitespace characters, no conversion is performed.

If No valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, Int_max (2147483647) or int_min ( -2147483648) is Returne D.

Note: Converts a string to an integer. Consider all possible input scenarios. This issue in the "C + + Primer classic" In the design of the calculator encountered, but did not carefully analyze each situation, and now to take a good look.

Solution: This is simply unreasonable, the list of some possible situations

Input: "+-2" expected:0

Input: "+ 1 2" expected:1

Input: "+ + 2" expected:0

Input: " -012a34" expected:12

Input: "+11191657170" expected:2147483647

Here is the code, which needs to be streamlined:

Long long result{0};    int n{0};//number of digits int sign{1}, p_n_flag{0}, zero_flag{0}; for (String::iterator s_i = Str.begin (); S_i! = Str.end (); s_i++) {if (*s_i = = '-') {if (p_n                _flag==0) {p_n_flag = 1;                sign =-1;            Continue            } else {sign = 0;//If two occurrences are considered illegal input break;                }} if (*s_i = = ' + ') {if (P_n_flag = = 0) {P_n_flag = 1;                sign = 1;            Continue                } else {sign = 0;            Break            }} if (*s_i = = ") {if (n = = 0 && Zero_flag = = 0 && p_n_flag==0)            {continue;}        else {break;}            } if (*s_i = = ' 0 ') {zero_flag = 1;         if (n = = 0) {continue;}   else {n = n + 1;                result = result * 10; if (Result > Int_max) {switch (sign) {case 0:                    return 0;                    Case 1:return Int_max;                    Case-1:return int_min;                    Default:break;                            }} else {continue;} }} if ((*s_i) > ' 0 ' && ((*s_i) < ' 9 ' | |            (*s_i) = = ' 9 ') {n = n + 1;            Result = result * + ((*s_i)-48);            Result = result * + ((*s_i)-' 0 ');//The last parenthesis does not use an explicit value of 48 instead of ' 0 ' cout << result << Endl; if (Result > Int_max) {switch (sign) {case 0:                return 0;                Case 1:return Int_max;       Case-1:return int_min;         Default:break;        }}} else {break;        }} result = Result*sign; return result;

Leetcode Title Finishing-2 Reverse integer && String to 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.