LeetCode [7--Integer Inversion] LeetCode [8--String to Integer]

Integer inversion

Title description

Given a 32-bit signed integer, you need to reverse the digits in each digit of this integer.
Insert picture description here

Problem-solving ideas

Take one bit for x% 10, the next one for x / 10, pay attention to crossing the boundary,

Code

class Solution {
public:
    int reverse(int x) {
        int sum = 0;
        
        while(x)
        {
            if(sum<INT_MIN/10) 
                return 0;
            if(sum>INT_MAX/10)
                return 0;
            sum=sum*10+x%10;
            x/=10;
        }
        return sum;
    }
};

String to integer

Title description

Please implement an atoi function to convert a string into an integer.

First, the function discards useless leading space characters as needed until the first non-space character is found.

When the first non-null character we find is a positive or negative sign, the symbol is combined with as many consecutive digits as possible to serve as the sign of the integer; if the first non-null character is The number is directly combined with the subsequent numeric characters to form an integer.

In addition to the valid integer part of the string, there may be extra characters, these characters can be ignored, they should not affect the function.

Note: If the first non-space character in the string is not a valid integer character, the string is empty, or the string contains only white space characters, then your function does not need to be converted.

In any case, if the function cannot perform a valid conversion, return 0.

Description:

Assuming that our environment can only store signed integers with a size of 32 bits, the value range is [−231, 231 − 1]. If the value exceeds this range, return INT_MAX (231 − 1) or INT_MIN (−231).

Problem-solving ideas

first step

Remove all spaces in the string and record only the subscripts of valid characters.
If they are all spaces, return 0 directly

int length = s.size();
int index = 0;
while(index < length)
{
	if(s[length] != ' ')
		break;
	index++;
}
if(index == length)
	return 0;

Second step

Judgment of positive and negative, indicated by a mark, initially positive

int signal = 1;
if(s[index] = '+')
	index++;
else if(s[index] = '-')
{	
	singal = -1;
	index++;
}

third step

Conversion, first determine whether it crosses the border, and then convert,

int res = 0; //保存结果
while(index<legnth)
{
	//1.取一个字符
	char Cur = s[index];
	//2.判断是否为数字字符
	if(Cur < '0' || Cur >'9')
		break;
	//3.判断是否越界
	if (res > INT_MAX / 10 || (res == INT_MAX / 10 && (curChar - '0') > INT_MAX % 10)) 
	{
          return INT_MAX;
    }
    if (res < INT_MIN / 10 || (res == INT_MIN / 10 && (curChar - '0') > -(INT_MIN % 10))) 
    {
           return INT_MIN;
    }
    //4.转换
    res = res*10 + signal*(Cur - '0');
    //5.取下一个字符
    index++;
}

Code

class Solution {
public:
    int myAtoi(string str) {
        unsigned long len = str.length();

        int index = 0;
        while (index < len) {
            if (str[index] != ' ') {
                break;
            }
            index++;
        }
        //如果全部是空格,直接返回0
        if (index == len) {
            return 0;
        }

        int sign = 1;
        if (str[index] == '+') {
            index++;
        } else if (str[index] == '-') 
        {
            sign = -1;
            index++;
        }

        int res = 0;
        while (index < len) {
            char curChar = str[index];
            if (curChar < '0' || curChar > '9') {
                break;
            }

            if (res > INT_MAX / 10 || (res == INT_MAX / 10 && (curChar - '0') > INT_MAX % 10)) {
                return INT_MAX;
            }
            if (res < INT_MIN / 10 || (res == INT_MIN / 10 && (curChar - '0') > -(INT_MIN % 10))) {
                return INT_MIN;
            }

            res = res * 10 + sign * (curChar - '0');
            index++;
        }
        return res;
    }
};
Published 253 original articles · praised 41 · 40,000+ views

Guess you like

Origin blog.csdn.net/liuyuchen282828/article/details/104569664