LeetCode_String to Integer (atoi)

本文详细阐述了如何使用C语言实现将字符数组转换为整数值的过程,包括跳过空格、识别正负号和数字字符,并正确处理溢出情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Implement atoi to convert a string to an integer.
int atoi (const char * str);
Convert string to integer
Parses the C-string str interpreting its content as an integral number, which is returned as a value of type int.

The function first discards as many whitespace characters (as in isspace) as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many base-10 digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed and zero is returned.
class Solution {
public:
    int atoi(const char *str) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
         int len = strlen(str) ;
         int i =0;
         long long result = 0;
         int flag = 1, b= 0;
         int numjia = 0;
         int numjian = 0;
     
         while(i<len && str[i] == ' ') i++;
        
         
         for(; i< len ; i++)
         {
            switch(str[i])
            {
                case '+': numjia++;break;
                case '-': numjian++; flag =-1;break;
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9': result+=str[i] - '0';result*=10;break;
                default: b = 1;break;
            }
            if(b==1) break;
            if(numjia>1 ||numjian>1)
                return 0;
         }
         
    
          result = result/10;
          result *= flag;
          
          if(result > INT_MAX) return INT_MAX;
          if(result < INT_MIN) return INT_MIN;
          return result;
    }
};

 重写后:

class Solution {
public:
    int atoi(const char *str) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
   
        if(str == NULL ) return 0;
        
        int len = strlen(str);
        long long res = 0;
        int cur = 0, flag = 1;
        
        while(cur<len && str[cur] == ' ') ++cur;
        
        if( str[cur] == '-' || str[cur] == '+'){
            if(str[cur] == '-')
              flag = -1;
            cur++;
        }
        
        while(cur < len){
            int c = str[cur] - '0';
            if(c<0 || c > 9)
                break;
            res = c + res*10; 
            ++cur;
        }
        res *= flag ;
            
        if(res > INT_MAX)
            return INT_MAX;
        if(res < INT_MIN)
          return INT_MIN;
          
        return res;
    }
};

 

转载于:https://www.cnblogs.com/graph/p/3217386.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值