Quick questions: integer reversal, string conversion to integer (atoi), palindrome number

It starts again today


1. Integer reversal

Original title link: https://leetcode.cn/problems/reverse-integer/

Given a 32-bit signed integer x, return the result of inverting the numeric part of x.

If the reversed integer exceeds the range of a 32-bit signed integer [−231, 231 − 1], 0 is returned.

Assume that the environment does not allow the storage of 64-bit integers (signed or unsigned).

Example 1:

Input: x = 123
Output: 321
Example 2:

Input: x = -123
Output: -321
Example 3:

Input: x = 120
Output: 21
Example 4:

Input: x = 0
Output: 0

hint:

-231 <= x <= 231 - 1

Code (C)


int reverse(int x){
    
    
    int i=0;
    while(x){
    
    
        if (i < INT_MIN / 10 || i> INT_MAX / 10) {
    
     //注意,循环的过程中就要判断是否会溢出
        return 0;
        }
        i=i*10+x%10;
        x=x/10;
    }
    return i;
}


2. String conversion to integer (atoi)

Question link: https://leetcode.cn/problems/string-to-integer-atoi/

Please implement a myAtoi(string s) function so that it can convert a string into a 32-bit signed integer (similar to the atoi function in C/C++).

The algorithm of the function myAtoi(string s) is as follows:

Read in the string and discard useless leading spaces.
Check whether the next character (assuming it's not the end of the character yet) is positive or negative, and read that character (if any). Determine whether the final result is a negative or positive number. If neither is present, the result is assumed to be positive.
Read in the next character until the next non-numeric character is reached or the end of the input is reached. The rest of the string is ignored.
Convert the numbers read in the previous steps into integers (i.e., "123" -> 123, "0032" -> 32). If no number is read in, the integer is 0. Change the symbols if necessary (start with step 2).
If the integer number exceeds the 32-bit signed integer range [−231, 231 − 1], the integer needs to be truncated to keep it within this range. Specifically, integers less than −231 should be fixed to −231, and integers greater than 231 − 1 should be fixed to 231 − 1.
Return an integer as the final result.
Notice:

The whitespace characters in this question only include the space character ' '.
Do not ignore any characters except leading spaces or the rest of the string after numbers.

Example 1:

Input: s = "42"
Output: 42
Explanation: The bold string is the character that has been read, and the caret is the currently read character.
Step 1: "42" (No characters are currently read, because there is no leading space)
^
Step 2: "42" (No characters are currently read, because there is no '-' or '+' here)
^
Step 3 : "42" (read "42")
^
Parse to get the integer 42.
Since "42" is in the range [-231, 231 - 1], the final result is 42.
Example 2:

Input: s = "-42"
Output: -42
Explanation:
Step 1: "-42" (read in the leading spaces, but ignore them)
^
Step 2: "-42" (read in the '-' character, so The result should be a negative number)
^
Step 3: "-42" (read "42")
^
Parsed to get the integer -42.
Since "-42" is in the range [-231, 231 - 1], the final result is -42.
Example 3:

Input: s = "4193 with words"
Output: 4193
Explanation:
Step 1: "4193 with words" (no characters are currently read in because there are no leading spaces)
^
Step 2: "4193 with words" (no characters are currently read in) character, because there is no '-' or '+' here)
^
Step 3: "4193 with words" (read "4193"; because the next character is not a number, the reading stops) ^Parse and get the integer
4193
.
Since "4193" is in the range [-231, 231 - 1], the final result is 4193.

hint:

0 <= s.length <= 200
s consists of English letters (uppercase and lowercase), numbers (0-9), ' ', '+', '-' and '.'

Code (C)

int myAtoi(char * s){
    
     
    int index=0,tag =0;//tag0表正数
    while(s[index]==' ')
        index++;//跳过所有空字符
    if(s[index]=='+'||s[index]=='-'){
    
    
        if(s[index]=='-')
            tag=1;
        index++;//统一后移
    }
    double sum =0;//测试可知,后面必需跟连续的数字字符,遇到其他任意非数字字符,结束  注意00可以开头 
    while(s[index]>='0'&&s[index]<='9'){
    
    
        sum+=(s[index]-'0');
        if(sum>=INT_MAX&&tag==0)//上下限
            return INT_MAX;
        if((0-sum)<=INT_MIN)
            return INT_MIN;
        if(s[index+1]>='0'&&s[index+1]<='9')
            sum*=10;
        index++;
    }
    if(tag==1)
        return (int)(0-sum);
    return (int)sum;
}


3. Number of palindromes

Original question link: https://leetcode.cn/problems/palindrome-number/You
are given an integer x. If x is a palindrome integer, return true; otherwise, return false.

A palindrome number is an integer that reads the same in forward order (from left to right) and reverse order (from right to left).

For example, 121 is a palindrome, but 123 is not.

Example 1:

Input: x = 121
Output: true
Example 2:

Input: x = -121
Output: false
Explanation: Reading from left to right, it is -121. Reading from right to left, it is 121-. Therefore it is not a palindrome.
Example 3:

Input: x = 10
Output: false
Explanation: Reading from right to left, it is 01. Therefore it is not a palindrome.

hint:

-231 <= x <= 231 - 1

Code (C)


bool isPalindrome(int x){
    
    
    long int a=x,b=0;
    if(x>=0&&x<10)
        return true;
    else if(x<0)
        return false;
    else while(a>0)
    {
    
    
        b=b*10+a%10;
        a=a/10;
    }
    if(b==x) return true;
    else return false;
}


Guess you like

Origin blog.csdn.net/weixin_62529383/article/details/133070060