当前位置:网站首页>剑指Offer(四十九):把字符串转换成整数
剑指Offer(四十九):把字符串转换成整数
2022-07-26 10:36:00 【康斯坦奇】
解法1:这道题要考虑全面,对异常值要做出处理。对于这个题目,需要注意的要点有:
·指针是否为空指针以及字符串是否为空字符串;
·字符串对于正负号的处理;
·输入值是否为合法值,即小于等于’9’,大于等于’0’;
·int为32位,需要判断是否溢出;
class Solution:
def StrToInt(self, s):
# write code here
if len(s) == 0:
return 0
else:
minus = False
flag = False
if s[0] == '+':
flag = True
if s[0] == '-':
flag = True
minus = True
begin = 0
if flag:
begin = 1
num = 0
minus = -1 if minus else 1
for each in s[begin:]:
if each >= '0' and each <= '9':
num = num*10 + minus*(ord(each) - ord('0'))
else:
num = 0
break
return num
边栏推荐
- Interview questions and answers for the second company (2)
- .NET操作Redis Set无序集合
- Asynctask < T> decoration and await are not used in synchronous methods to obtain asynchronous return values (asynchronous methods are called in synchronous methods)
- [leetcode每日一题2021/4/29]403. 青蛙过河
- 粽子大战 —— 猜猜谁能赢
- sigmod 函数与softmax 函数对比
- 构造器、方法重载、对象数组和static
- [leetcode每日一题2021/2/14]765. 情侣牵手
- json_object_put: Assertion `jso->_ref_count > 0‘ failed.Aborted (core dumped)
- 事务的传播性propagation
猜你喜欢
![[leetcode每日一题2021/2/13]448. 找到所有数组中消失的数字](/img/9b/624416fa6a408bf64ca5438273176b.png)
[leetcode每日一题2021/2/13]448. 找到所有数组中消失的数字

canvas上传图片base64-有裁剪功能-Jcrop.js

vscode上使用anaconda(已经配置好环境)

Issue 6: which mainstream programming language should college students choose

第7期:内卷和躺平,你怎么选

多目标优化系列1---NSGA2的非支配排序函数的讲解

Application of.Net open source framework in industrial production

Redis Docker实例与数据结构
![[leetcode每日一题2021/4/23]368. 最大整除子集](/img/0b/32ca862963c842a93f79eaac94fb98.png)
[leetcode每日一题2021/4/23]368. 最大整除子集

Uniapp uses the simple method signalr (only for web debugging, cannot package apps)
随机推荐
Tradingview 使用教程
图片随手机水平移动-陀螺仪。360度设置条件
2022pta usual training questions (1-10 string processing questions)
Oracle cannot start tnslistener service cannot start
oracle 启动不了 tnslistener服务启动不了
[leetcode每日一题2021/5/8]1723. 完成所有工作的最短时间
Comparison of packet capturing tools fiddler and Wireshark
13 以对象管理资源
Issue 7: how do you choose between curling up and lying flat
记给esp8266烧录刷固件
2022pta平时训练题(1~10题字符串处理问题)
A semicolon is missing
.NET操作Redis Set无序集合
Navicat15 MySQL (centos7) connected to local virtual machine
使用Geoprocessor 工具
[Halcon vision] morphological corrosion
STM32 Alibaba cloud mqtt esp8266 at command
Issue 5: the second essential skill for College Students
Parallelism, concurrency and several directions for high concurrency optimization
js 获得当前时间,时间与时间戳的转换