8. String to Integer (atoi) --重要
来源:互联网 发布:淘宝捉猫猫进口鱼罐头 编辑:程序博客网 时间:2024/06/06 04:40
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.
Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.
以下代码仅仅考虑了空格的问题,以及溢出的问题,没有考虑不合法输出的问题,以及进制的问题
int myAtoi(string str) { long result = 0; int indicator = 1; for(int i = 0; i<str.size();) { i = str.find_first_not_of(' '); //第一个i的位置是第一个非空的位置 if(str[i] == '-' || str[i] == '+') indicator = (str[i++] == '-')? -1 : 1; //正负号的处理,即正负数的处理 while('0'<= str[i] && str[i] <= '9') { result = result*10 + (str[i++]-'0'); if(result*indicator >= INT_MAX) return INT_MAX; //判断是否溢出 if(result*indicator <= INT_MIN) return INT_MIN; } return result*indicator; } }
int myAtoi(string str) { int index=0; while(str[index]==' ')index++; int flag=1; if(str[index]=='-'){ index++; flag=-1; }else if(str[index]=='+'){ index++; } int res=0; while(index<str.size()){ if(str[index]<'0' || str[index]>'9'){ return flag*res; } int digit=str[index]-'0'; if(flag==1 && res*10.0+digit>INT_MAX){ return INT_MAX; }else if(flag==-1 && -(res*10.0+digit)<INT_MIN){ return INT_MIN; } res = res*10+digit; index++; } return flag*res; }
剑指offer中则考虑的全面些,考虑全面的代码如下:
enum status {kvalid=0,kinvalid}; //该枚举型变量用来标示如果返回num=0的情况下,字符串是合法输入还是非法输入,初始值kvalid =0(合法输入)//kinvalid =1(非法输入),合法非法的状态存储在全局变量g_status中 int g_status = kvalid; int strToIntCore(string str, int start, int end, bool minus){ int flag = minus?-1:1; long long num = 0; while(str[start] != '\0' && start<=end){ if(str[start] >= '0' && str[start] <= '9'){ num = num * 10 + flag * (str[start] - '0'); if(num > INT_MAx || num < INT_MIN){ //判断数字是否溢出 ,溢出至num=0 num = 0; break; } start++; } else{ //不合法输入 num = 0; break; } } if(str[start] == '\0'){ g_status = kvalid; } return num;}int strToInt(string str){ int g_status = kinvalid; int len = str.length(); int start = 0; int end = len - 1; long long num = 0; bool minus = false;//标示正负数 if(len >= 1){ while(str[start] == ' ') //处理一开始的空格 ,诸如“ +4500” start++; if(str[start] == '+'){ start++; } else if(str[start] == '-'){ start++; minus = true; } if(start <= end){ num = strToIntCore(str,start,end,minus); } } return num;}
其实还可以考虑进制的因素:设置一个radius记录进制
num = num * radius + str[i] - ‘0’,即可;
其中16进制的数字开头为“0X”
8进制的数字开头为”0”
剩下设置为十进制的数字
- 8. String to Integer (atoi) (重要)
- 8. String to Integer (atoi) --重要
- 8.String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8.String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 8. String to Integer (atoi)
- 【Python正则表达式】 批量去除视频名称中的网址
- C++---笔记1
- Linux日常命令记录
- 内存泄露检测方法介绍
- 【Java】二维数组
- 8. String to Integer (atoi) --重要
- GNU计划
- html5学习--静态页面传递参数的几种方法
- MySQL入门
- HRBUST
- iOS Apns推送消息Json格式
- html5中的input元素
- 算法<改进的冒泡排序、直接插入排序、折半插入排序、希尔排序、快速排序、归并排序>
- c++第二次实验项目:两点距离