当前位置:网站首页>剑指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
边栏推荐
- 一些你不知道的 web API
- MD5 encryption
- 点击el-dropdown-item/@click.native
- Uniapp uses the simple method signalr (only for web debugging, cannot package apps)
- .net5wtm (asp.net core) PgSQL unpacking operation
- Oracle cannot start tnslistener service cannot start
- js,e.pageX、pageY模态框拖动
- Introduction to data analysis | kaggle Titanic mission (I) - > data loading and preliminary observation
- What if MySQL can't get in
- 剑指Offer(九):变态跳台阶
猜你喜欢

Issue 8: cloud native -- how should college students learn in the workplace
The software cannot be opened

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

putty的使用教程

Zongzi battle - guess who can win

js翻页、kkpager.js翻页

uniapp使用简单方法signalR(仅用于web调试,无法打包app)

Write to esp8266 burning brush firmware
![[leetcode每日一题2021/2/14]765. 情侣牵手](/img/be/8639a05c733638bf0b3fdeb11abccf.png)
[leetcode每日一题2021/2/14]765. 情侣牵手

Redis Docker实例与数据结构
随机推荐
文案秘籍七步曲至----文献团队协作管理
点击el-dropdown-item/@click.native
构造器、方法重载、对象数组和static
[Halcon vision] morphological expansion
【论文下饭】Deep Mining External Imperfect Data for ChestX-ray Disease Screening
Asynctask < T> decoration and await are not used in synchronous methods to obtain asynchronous return values (asynchronous methods are called in synchronous methods)
第5期:大学生入职必备技能之二
Redis implementation of distributed lock solution
Uninstall Meizu app store
.net operation redis string string
datav漂亮数据屏制作体验
C语言回调函数
js,e.pageX、pageY模态框拖动
关于硕博士开题报告编写的思考
剑指Offer(十):矩形覆盖
分布式锁解决方案之Redis实现
移动端H5开发常用技巧总结
.net operation redis list list
Inheritance method of simplified constructor (II) - class inheritance in ES6
异常的概念与处理