题目


Inplement atoi which converts a string to an integer.

The function first discards as many whitespace characters 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 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.

Note:

  • Only the space character ' ' is considered as whitespace character.
  • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [ \(−2^{31}, 2^{31} − 1\) ]. If the numerical value is out of the range of representable values, INT_MAX ( \(2^{31} − 1\) ) or INT_MIN ( \(−2^{31}\) ) is returned.

思路


思路1

问题的关键在于必须满足各种条件:

  • 开头的空格
  • 正负号的处理
  • 溢出判断
  • 数字的处理

思路2:正则表达式(Python)

  • str.strip(rm) 删除str字符中开头和结尾处,位于rm序列的字符
  • str.lstrip(rm) 删除str字符中开头处,位于rm序列的字符
  • str.rstrip(rm) 删除str字符中结尾处,位于rm序列的字符
  • 利用try-except块来检查异常输入
  • 正则表达式,re模块

    \d 表示[0,9]的数字,\d+ 表示不止一个数字

    ^ 表示匹配字符串的开头

    -?表示匹配前一个字符或子表达式0次或1次重复

    re.search 扫描整个字符串并返回第一个成功的匹配,匹配成功返回一个匹配的对象,否则返回None

    group() 分组就是用()括起来的正则表达式,匹配出的内容表示一个分组。group()输出一个包含这个组所对应的元组。

cpp

class Solution {
public:
int myAtoi(string str) { int p=0;
while(str[p]==' '){
p++;
} int sign=1;//判断正负
if(str[p]=='+'){
p++;
}
else if(str[p]=='-'){
sign=-1;
p++;
} long resInt=0;
for(int i=p;i<str.size();i++){
if(str[i]< '0' || str[i]> '9'){
break;
}
int temp=str[i]-'0';//转化为数字
resInt = 10*resInt+temp;
if(sign==1){
if(resInt >= INT_MAX){
return INT_MAX;
}
}
else{
if(resInt-1 >= INT_MAX){
return INT_MIN;
}
}
}
resInt = sign*resInt;
return resInt;
} };

Python

class Solution(object):
def myAtoi(self, str):
"""
:type str: str
:rtype: int
"""
str = str.strip()
try:
res = int(re.search('(^[\+\-]?\d+)', str).group())
except:
res = 0
return min(max(-2147483648, res), 2147483647)

8. String to Integer[M]字符串转整数的更多相关文章

  1. [LeetCode] String to Integer (atoi) 字符串转为整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  2. [leetcode]8. String to Integer (atoi)字符串转整数

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

  3. [LeetCode] 8. String to Integer (atoi) 字符串转为整数

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

  4. 【LeetCode】8. String to Integer (atoi) 字符串转换整数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...

  5. Leetcode8.String to Integer (atoi)字符串转整数(atoi)

    实现 atoi,将字符串转为整数. 该函数首先根据需要丢弃任意多的空格字符,直到找到第一个非空格字符为止.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字 ...

  6. 【LeetCode】8. String to Integer (atoi) 字符串转整数

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  7. 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))

    这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...

  8. 008 String to Integer (atoi) 字符串转换为整数

    详见:https://leetcode.com/problems/string-to-integer-atoi/description/ 实现语言:Java class Solution { publ ...

  9. Leetcode8--->String to Integer(实现字符串到整数的转换)

    题目: 实现字符串到整数的转换 解题思路: 下面给出这道题应该注意的一些细节: 1. 字符串“    123   ” = 123: 2.   字符串“+123” = 123: 3.   字符串“-12 ...

随机推荐

  1. JsonResult

    注意:如果有大量的json数据要处理,建议使用ASP .NET Web API框架处理,专门用来处理json数据交换并提供跨平台访问能力的 ConentType设置为application/json ...

  2. vue 返回上一页在原来的位置

    http://www.jb51.net/article/118592.htm http://blog.csdn.net/qq_26598303/article/details/51189235 htt ...

  3. Java中的强制转换

    特点: 1.需要程序员手动修改代码 2.语法:范围小的类型 变量名 = (范围小的类型)范围大的类型的数据 3.从范围小 到 范围大  注意: 强制类型转换可能会造成数据的丢失哦,小伙伴们在应用时一定 ...

  4. 【转】ROI Pooling

    Faster rcnn的整体构架: 训练的大致过程: 1.图片先缩放到MxN的尺寸,之后进入vgg16后得到(W/16,H/16)大小的feature map: 2.对于得到的大小为(W/16,H/1 ...

  5. nyoj8-一种排序

    一种排序 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复:还知道这个长方形的宽和长,编号.长.宽都是整数:现 ...

  6. Java包名称中通配符的含义

    "com.abc 表示的意义为:系统从com.abc这个包及其子孙包扫描组件 "com.abc.* 表示的意义为:系统从com.abc这个包的子孙包扫描组件

  7. MYSQL存储过程初步认知

    存储过程(Stored Procedure): 一组可编程的函数,是为了完成特定功能的SQL语句集,经编译创建并保存在数据库中,用户可通过指定存储过程的名字并给定参数(需要时)来调用执行. 优点:将重 ...

  8. 《黑白团团队》第八次团队作业:Alpha冲刺 第三天

    项目 内容 作业课程地址 任课教师首页链接 作业要求 团队项目 填写团队名称 黑白团团队 填写具体目标 认真负责,完成项目 团队项目Github仓库地址链接. 第三天 日期:2019/6/17 成员 ...

  9. PHP学习总结(9)——PHP入门篇之WAMPServer服务控制面板介绍

    及MySQL数据库的整合软件包.免去了开发人员将时间花费在繁琐的配置环境过程,从而腾出更多精力去做开发.在windows下将Apache+PHP+Mysql 集成环境,拥有简单的图形和菜单安装和配置环 ...

  10. vue+Ueditor集成 [前后端分离项目][图片、文件上传][富文本编辑]

    后端DEMO:https://github.com/coderliguoqing/UeditorSpringboot 前端DEMO:https://github.com/coderliguoqing/ ...