分析这道题,输入数据有如下几种情况:

第一类:输入字符串无法转换为整数

这一类包含以下几种情况:

  1. 输入字符串为空
  2. 开头字符为数字、符号(+,-)、空格以外的字符
  3. 有多个加减符号的字符串
  4. 符号没有紧跟数字
  5. 字符串中没有数字

以上这几种情况直接返回 0

第二类: 输入字符串部分可以转换

这类情况中,数字后如出现其他不是数字的字符,那么该符号出现位置后的所有字符无效

第三轮: 可以全部转换

这类该怎么转就怎么转

参考代码如下:

class Solution:
def myAtoi(self, str):
"""
:type str: str
:rtype: int
"""
raw_str = str
# set of valid
valid_set = {
'', '', '', '', '', '', '', '', '', '', '+', '-', ' '
}
# set of num
num_set = {'', '', '', '', '', '', '', '', '', ''}
# set of sign
sign_set = {'+', '-'}
# set of space k = # current location
m = # the number of signs
p = # the last space location
n = # the last signs location
i = # the number of 'num' temp_str = '' # case1: str is Null
if len(raw_str) == :
return # case2: illegal words at begining
if raw_str[] not in valid_set:
return for s in raw_str:
if s in sign_set:
# the sign after num is not valid
if i > :
break m = m +
n = k
# case3: if there are more than signs
if m > :
return
if s == ' ':
# the space after num is not valid
if i > :
break
p = k if s in num_set:
# case4: if the last sign location before last space location
if p > n and m > :
return
i = i +
temp_str = temp_str + s if s not in valid_set:
k = k +
break k = k + # case5: have no number in str:
if i == :
return
else:
# the num with sign
if m > :
temp_str = raw_str[n] + temp_str covert_int = int(temp_str) # overflow
if covert_int >= ** - :
return ** -
if covert_int <= (-**):
return (-**) return covert_int # test
s = Solution()
print(s.myAtoi("-42"))
参考:
 
 
 
 
 

leetcode题目讲解(Python):字符串转整数 (atoi)的更多相关文章

  1. 前端与算法 leetcode 8. 字符串转换整数 (atoi)

    目录 # 前端与算法 leetcode 8. 字符串转换整数 (atoi) 题目描述 概要 提示 解析 解法一:正则 解法二:api 解法二:手搓一个api 算法 传入测试用例的运行结果 执行结果 G ...

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

    8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...

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

    8. 字符串转换整数 (atoi) 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组 ...

  4. 字符串转换整数 (atoi) C++实现 java实现 leetcode系列(八)

    字符串转换整数 (atoi) java实现 C++实现 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当 ...

  5. LeetCode8. 字符串转整数 (atoi)

    8. 字符串转整数 (atoi) 描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连 ...

  6. Leetcode(8)字符串转换整数

    Leetcode(8)字符串转换整数 [题目表述]: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我 ...

  7. 17、字符串转换整数 (atoi)

    17.字符串转换整数 (atoi) 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非 ...

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

    8. 字符串转换整数 (atoi) 方法一 import re import math class Solution(object): def myAtoi(self, str): "&qu ...

  9. leetcode刷题笔记08 字符串转整数 (atoi)

    题目描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即 ...

  10. [LeetCode] 8. 字符串转换整数 (atoi)

    题目链接:https://leetcode-cn.com/problems/string-to-integer-atoi/ 题目描述: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先 ...

随机推荐

  1. Redis_初识

    一.简介 Redis(Remote Dictionary Server)本质上是一个Key-Value类型的内存数据库,整个数据库统统加载在内存当中进行操作,定期通过异步操作吧数据库flush到硬盘上 ...

  2. Flask入门到放弃(五)—— 蓝图

    转载请在文章开头附上原文链接地址:https://www.cnblogs.com/Sunzz/p/10980094.html 蓝图 Blueprint 模块化 随着flask程序越来越复杂,我们需要对 ...

  3. CentOS安装MySQL8.x

    MySQL的安装 (4,5,6可省略) 声明:CentOS版本为7.6,安装的MySQL版本为8.0.17 1. 首先要卸载掉本机自带的mysql相关,包括MariaDB. rpm -pa | gre ...

  4. 利用mysql的inet_aton()和inet_ntoa()函数存储IP地址的方法

    原文:https://www.jb51.net/article/29962.htm 当前很多应用都适用字符串char(15)来存储IP地址(占用16个字节),利用inet_aton()和inet_nt ...

  5. 洛谷 P1032 字串变换题解

    题目链接:https://www.luogu.org/problem/P1032 题目描述 已知有两个字串A,BA,B及一组字串变换的规则(至多66个规则): A_1A1​ ->B_1B1​ A ...

  6. Tarjan算法分解强连通分量(附详细参考文章)

    Tarjan算法分解强连通分量 算法思路: 算法通过dfs遍历整个连通分量,并在遍历过程中给每个点打上两个记号:一个是时间戳,即首次访问到节点i的时刻,另一个是节点u的某一个祖先被访问的最早时刻. 时 ...

  7. Educational Codeforces Round 64 (Rated for Div. 2)题解

    Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...

  8. python list 字符串排序

    #coding:utf-8 import re s = ['dat2','dat10','dat5'] #方法一 new = sorted(s,key = lambda i:int(re.search ...

  9. 项目Alpha冲刺--8/10

    项目Alpha冲刺--8/10 作业要求 这个作业属于哪个课程 软件工程1916-W(福州大学) 这个作业要求在哪里 项目Alpha冲刺 团队名称 基于云的胜利冲锋队 项目名称 云评:高校学生成绩综合 ...

  10. zookeeper 的 docker 镜像使用

    dockerhub 网址:https://hub.docker.com/_/zookeeper