8、String to Integer (atoi)
2015-12-02 21:38
381 查看
题目:
mplement 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
accepts a
reset your code definition.
spoilers alert... click to show requirements for atoi.
解题思路:
1. 字串为空,返回0;
2. 字串的前缀空格需要忽略掉;
3. 遇到的第一个字符,如果是‘+’或‘-’号,将符号标识为相应值;
4. 处理数字;
5. 在上述处理过程中,如果转换出的值超出了int型的范围,就返回int的最大值或最小值。
C++版本:
python版本:
mplement 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.
spoilers alert... click to show requirements for atoi.
解题思路:
1. 字串为空,返回0;
2. 字串的前缀空格需要忽略掉;
3. 遇到的第一个字符,如果是‘+’或‘-’号,将符号标识为相应值;
4. 处理数字;
5. 在上述处理过程中,如果转换出的值超出了int型的范围,就返回int的最大值或最小值。
C++版本:
class Solution { public: int myAtoi(string str) { long long res = 0; int minus = 1; if(str!="") { while(str.size()>0 and str[0]==' ') //正负号前面有空格 如" -123" str.erase(str.begin()); if(str[0]=='-') //正负号处理 { minus=-1; str.erase(str.begin()); } else if(str[0]=='+') { minus=1; str.erase(str.begin()); } } for(char c : str) { if(res>INT_MAX) //超出int 范围 break; else if(isdigit(c)) //如果是数字 res = 10*res + c-'0'; else //特殊符号处理 如"-+123" -> 0 return minus*res; } if(minus*res>INT_MAX) return minus*INT_MAX; if(minus*res<INT_MIN) return minus*INT_MIN; return minus*res; } };
python版本:
class Solution(object): def myAtoi(self, str): """ :type str: str :rtype: int """ INT_MAX = 2147483647; INT_MIN = -2147483648 index,res = 0,0 sign = True if(str==""):return 0; while(index<len(str) and str[index]==" "): index += 1 if(index<len(str) and (str[index]=="+" or str[index]=="-")): if(str[index]=="-"): sign = False index += 1 while(index<len(str) and str[index]>='0' and str[index]<='9'): res = 10*res+int(str[index]) if(res>INT_MAX): if(sign):return INT_MAX else:return INT_MIN index += 1 if(sign==False):return -res return res
相关文章推荐
- scala None null Nil的说明
- BZOJ 1012:[JSOI2008]最大数 maxnumber
- socket通讯原理以及tcp、ip三次握手机制学习
- IronPython for ASP.NET 部署注意事项
- jQuery基础选择器
- 思维导图 JAVA 语法基础
- Http中的get和post的区别
- 并集、交集、差集的概念
- oracle触发器调试
- 监听系统软键盘的显示与隐藏
- hdu1728逃离迷宫【深搜数量大小剪枝】
- WIN7下通过命令调用“控制面板项” / RUNDLL32详解 / CPL文件详解
- 理解 RPO 与 RTO
- tomcat服务器发布网站的方式小结
- 继承
- (二十三)变量名的命名
- (二十二)第二章最后的编程练习答案(七合一版)
- private, public, protected 访问标号的访问范围之JAVA篇
- SparkSQL样例
- leetcode 283 Move Zeroes C++