leetcode-easy-string- 8 String to Integer (atoi)
mycode 98.26%
易错点: while循环式,and判断的地方先判断下标会不会超出范围
class Solution(object):
def myAtoi(self, str):
"""
:type str: str
:rtype: int
"""
str = str.strip()
if not str :
return 0
res , start = '' , 0
if str[0] == '-' or str[0] == '+' :
res = str[0]
start = 1
if start >= len(str) or not str[start].isnumeric() :
return 0
while start < len(str) and str[start].isnumeric():
res += str[start]
start += 1
res = int(res)
if res > 2147483647 :
res = 2147483647
elif res < -2147483648:
res = -2147483648
return res
参考
思路:哪些情况下可以直接返回0呢 1) 空 2)+-符号不是开头3) 遍历到的字符不是空格、+-、数字
def myAtoi(self, S):
"""
:type str: str
:rtype: int
"""
res = '' S1 = S.strip() # need to know for s in S1:
if s == '': continue
if res != '' and s in '+-': break
if s in '-+0123456789': # need to know
res += s
else:
break if res == '' or res == '+' or res== '-':
return 0
elif int(res) < -2**31:
return -2**31
elif int(res) > (2**31)-1:
return (2**31) -1
else:
return int(res)
leetcode-easy-string- 8 String to Integer (atoi)的更多相关文章
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- 【一天一道LeetCode】#8. String to Integer (atoi)
一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...
- LeetCode: String to Integer (atoi) 解题报告
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))
8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- 【leetcode】8. String to Integer (atoi)
题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...
- leetcode第八题 String to Integer (atoi) (java)
String to Integer (atoi) time=272ms accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...
- leetcode第八题--String to Integer (atoi)
Problem: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible inp ...
随机推荐
- 14 Scrapy中selenium的应用
在通过scrapy框架进行某些网站数据爬取的时候,往往会碰到页面动态数据加载的情况发生,如果直接使用scrapy对其url发请求,是绝对获取不到那部分动态加载出来的数据值.但是通过观察我们会发现,通过 ...
- 上海的Costco,谈谈你的理解和感受
众所周知,Costco在上海第一天开业,由于人流量过大,一度暂停营业.我觉得Costco的成功在于不走寻常路,换位思考(站在用户.厂商角度看问题),下面几点是我觉得它做得比较独特的地方: 1. Cos ...
- 记一些使用mpvue时遇到的问题
一.在mpvue中使用vuex(和在vue中使用不同) 1.vue中使用vuex,在main.js中: import store from './store' new Vue({ store }) ...
- KPI VS OKR
近几年,OKR 这个词越来越流行了. 在硅谷,Google.Facebook.Amazon.LinkedIn 等公司都陆续成功落地了 OKR,国内的互联网巨头们,腾讯.百度.滴滴.小米等互联网公司也都 ...
- 帝国cms 通过文章的id获取信息
获取栏目id为13下id为46的数据 [e:loop={"select * from phome_ecms_news where classid = 13 and id = 46" ...
- react 不同环境配置不同域名
npm eject 先将配置文件暴露出来 将scripts中的build文件复制一份,改名为你需要的名字 将其中的 process.env.NODE_ENV 赋值为你需要的环境 在package.js ...
- 关于IDEA顶部栏隐藏问题,
那天手残,点到了 IDEA顶部菜单栏 > View > Appearance >Main Menu ,然后取消了勾选 然后就成了这个样子,没了顶部栏,恢复不过来,不知道如何进行设置 ...
- Tomcat配置JNDI
JNDI是什么?使用JNDI有什么好处? JNDI是 Java 命名与目录接口(Java Naming and Directory Interface),在J2EE规范中是重要的规范之一. 我个人对j ...
- 2019-2020-1 20199319《Linux内核原理与分析》第八周作业
可执行程序工作原理 ELF目标文件格式 1.目标文件(ABI,应用程序二进制接口):编译器生成的文件. 2.目标文件的格式:out格式.COFF格式.PE(windows)格式.ELF(Linux)格 ...
- 【The 13th Chinese Northeast Collegiate Programming Contest E题】
题目大意:给定一棵 N 个点的树,边有边权,定义"线树"为一个图,其中图的顶点是原树中的边,原树中两条有公共端点的边对应在线图中存在一条边,边权为树中两条边的边权和,求线图的最小生 ...