ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

8. 字符串转换整数 (atoi)

2021-07-18 14:05:24  阅读:170  来源: 互联网

标签:字符 whitespace no 整数 atoi str 字符串 input


目录

题目描述

Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself
what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely(含糊地,不详细地) (ie, no given input specs(说明,规范)). You are responsible to gather all the input requirements up front.

spoilers(剧透) alert... click to show requirements for atoi.

Requirements for atoi:
The function first discards as many whitespace characters(空白字符是指在屏幕上不会显示出来的字符(如空格,制表符tab,回车换行等)) as necessaryuntil the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical 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.

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 returned.

思路

转换前要跳过左边的空白字符,得到的第一个非空白字符要先判断是否是到达了字符串的末尾,如果到达了末尾,可以直接下结论返回;如果没有到达字符串的末尾,那它就是一个合法的非空白字符,要先判断是否是符号位,进行过符号位的判断操作之后,就是计算整数值(针对数字字符而言)直到越过数字字符的右边界,计算过程停止,返回计算结果

代码实现

class Solution {
public:
    int myAtoi(const char *str) {
    
        if(str==nullptr) 
        	return 0;
        const char *p = str;
        while(isspace(*p)) p++;
        if(*p == '\0')//可以直接下结论
        	return 0;
        int sign = 1;
        if(*p == '-')//如果没有符号字符不会进入这个条件判断操作,这个
        {            //操作不会发生
        	sign = -1;
        	p++;
        }
        else if(*p == '+')
        	p++;
        long ret = 0;//一个合理的初始化值
        while(*p!='\0')
        {
        	if(0<= *p-'0' && *p-'0'<=9)
        	{
        		ret = 10*ret+sign*(*p-'0');
        		if(ret >= INT_MAX)
        			return INT_MAX;
        		else if(ret <= INT_MIN)
        			return INT_MIN;
			p++;
        	}
        	else
        		return (int)ret;//已经越过了合法数字字符的右边界
        }
        return (int)ret;     
    }
};

标签:字符,whitespace,no,整数,atoi,str,字符串,input
来源: https://www.cnblogs.com/zjuhaohaoxuexi/p/15026398.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有