Likou Brushing Hundred Days Plan Day6 String Conversion Integer (atoi) C#

learning target:

I will continue to update my unique algorithm ideas, hoping to bring you different thinking expansion!
If you find it helpful, please like, follow and support!
Your encouragement is what keeps me going!
! ! !

Likou Question Bank Question 8 Official Link


Learning Content:

String to Integer (atoi)

请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数)。

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

Read in the string and discard useless leading spaces Check if the next character (assuming the end of the character is not reached) is positive or negative, read that character (if any). Determines whether the final result is negative or positive.
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 will be ignored.
Convert these numbers read in the previous steps to integers (ie, "123" -> 123, "0032" -> 32). If no number is read, the integer is 0
. Change the symbols if necessary (from step 2). If the integer number exceeds the 32-bit signed integer range [−231, 231 − 1]
, the integer needs to be truncated to stay 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 . Returns an integer as the final result. Notice:

Whitespace characters in this question only include the space character ' ' .
Do not omit any other characters except leading spaces or the rest of the string after a number.

Example 1:

Input: s = “42” Output: 42 Explanation: The bolded character string is the character that has been read in, and the caret is the character that is currently read. Step 1
: "42" (no characters are currently read because there are no leading spaces)
^ Step 2: "42" (No characters are currently read because there is no '-' or '+')
^ Step 3 : "42" (reads in "42")
^ parses to 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 leading spaces, but ignore)
^ Step 2: "-42" (read '-' characters, so The result should be a negative number)
^ Step 3: "-42" (reads in "42")
^ parses 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 because there are no leading spaces)
^ Step 2: "4193 with words" (no current characters are read in ) character, since there is no '-' or '+')
^ Step 3: "4193 with words" (reads "4193"; reading stops because the next character is not a number)
^ The integer 4193 is parsed. Since "4193" is in the range [-231, 231 - 1], the final result is 4193.

Example 4:

Input: s = "words and 987" Output: 0 Explanation: Step 1: "words and
987" (no characters are currently read because there are no leading spaces)
^ Step 2: "words and 987" (no current characters are read in ) character, because there is no '-' or '+')
^ Step 3: "words and 987" (reading stops because the current character 'w' is not a digit)
^ parses to integer 0, because no reading any number. Since 0 is in the range [-231, 231 - 1], the final result is 0.

Example 5:

Input: s = "-91283472332" Output: -2147483648 Explanation: Step 1
: "-91283472332" (currently no character is read because there is no leading space)
^ Step 2: "-91283472332" ('-' character is read , so the result should be a negative number)
^ Step 3: "-91283472332" (reads in "91283472332")
^ Parses to the integer -91283472332. Since -91283472332 is less than the lower bound of the range [-231, 231 - 1], the final result is truncated to -231 = -2147483648.

提示:

0 <= s.length <= 200
s 由英文字母(大写和小写)、数字(0-9)、' ''+''-''.' 组成

Source: LeetCode
Link: https://leetcode-cn.com/problems/string-to-integer-atoi

study-time:

2022.1.12


Learning output:

idea one
insert image description here

Problem- solving ideas
1. First of all, this question has little to do with the algorithm. It mainly examines our ability to analyze and process data.
2. We need to read the question carefully. I listed the following situations.
<1> Space + character + Number
<2>space+number+character
<3>character+space+number
<4>space+number+character
This may be interspersed with "+ -" sign
3. We must be clear, we first remove the space, and then start reading the, if it is normal to read the sign at this time, in addition, if the sign is read in any case, it is in the wrong position, just return the current value.
4. If the first character is read when reading the number, Then return directly. If you read a number, and then read a character after the number, you will also return directly. Anyway, if you encounter a number that is not a number, you will directly return to
5. If you read a number, at this time, you can read the plus and minus signs and spaces and letters. quit!

public class Solution {
    
    
    public int MyAtoi(string s) {
    
    
        int end=0;   //最终返回的值
        int index=0;
        bool isSignal=false;   //是否读到了正负号
        bool isNumber=false;     //是否读到了数字
        bool isBiggerZero=true;   //正负号的取值
        while(index<s.Length){
    
    
            if(s[index]==' '){
    
    
                if(isNumber){
    
       //如果读到了数字,再读到空格,就直接返回
                    return(EndDo());
                }
                if(isSignal){
    
      //如果读到正负号,再读到空格,直接返回
                    return(EndDo());
                }
                index++;
                continue;
            }
            if(s[index]=='+'){
    
    
                if(isNumber){
    
        //如果读到了数字,再读到正负号,直接返回
                    return(EndDo());
                }
                if(isSignal)return 0;   //如果已经读到了正负号,又读到,直接返回
                isBiggerZero=true;
                index++;
                isSignal=true;
                
                continue;
            }
            if(s[index]=='-'){
    
    
                if(isNumber){
    
       //如果读到了数字,再读到正负号,直接返回
                    return(EndDo());
                }
                if(isSignal)return 0; //如果已经读到了正负号,又读到,直接返回
                isBiggerZero=false;
                index++;
                    isSignal=true;
                 
                continue;
            }

            if(s[index]>'9'||s[index]<'0'){
    
    
                //是字符 退出
                return(EndDo());  //如果读到字符,直接退出

            }else{
    
    
                isNumber=true;
                //if(end==0&&s[index]=='0')return 0;
                if (isBiggerZero&&( end > 214748364 || (end == 214748364 && s[index] > '7')))  //这里做是否正越界判断
                {
    
    
                    return 2147483647;
                }
                
                if (!isBiggerZero&&( end > 214748364 || (end == 214748364 && s[index] >'8')))  //这里做是否负越界判断
                {
    
    
                    return -2147483648;
                }
                end*=10;
                end+=s[index]-'0';
            }
            
            index++;
            
        }
        return(EndDo());

        int EndDo(){
    
    
                if(isBiggerZero)return end;
            else return -end;
        }
    }
}

Author: guinea pig Xiaohuihui
The copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin blog.csdn.net/m0_48781656/article/details/122483523