当前位置:网站首页>Sword finger offer (49): convert a string to an integer
Sword finger offer (49): convert a string to an integer
2022-07-26 10:46:00 【Konstanch】
solution 1: This problem should be considered comprehensively , Deal with outliers . For this subject , The key points to be noted are :
· Whether the pointer is a null pointer and whether the string is an empty string ;
· Handling of signs in strings ;
· Whether the input value is a legal value , Less than or equal to ’9’, Greater than or equal to ’0’;
·int by 32 position , It is necessary to judge whether it overflows ;
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
边栏推荐
- Error[pe147]: declaration is incompatible with 'error problem
- 使用float实现左中右布局,中间内容自适应
- SQL Server 之Sql语句创建数据库
- 10 let operator= return a reference to *this
- mysql20210906
- 解决org.apache.commons.codec.binary.Base64爆红问题
- Error[Pe147]: declaration is incompatible with '错误问题
- [leetcode daily question 2021/2/13]448. Find all the missing numbers in the array
- 一文详解Nodejs中fs文件模块与path路径模块
- RT-Thread 学习笔记(三)---用SCons 构建编译环境
猜你喜欢

SAP ABAP 守护进程的实现方式

Issue 7: how do you choose between curling up and lying flat
![[leetcode daily question 2021/4/23]368. Maximum divisible subset](/img/0b/32ca862963c842a93f79eaac94fb98.png)
[leetcode daily question 2021/4/23]368. Maximum divisible subset

Issue 6: which mainstream programming language should college students choose

Disable usbjatg in Altium Designer

sigmod 函数与softmax 函数对比

20210807#1 C语言程序结构

Anaconda is used on vscode (the environment has been configured)

0x00007FFD977C04A8 (Qt5Sqld.dll)处(位于 a.exe 中)引发的异常: 0xC0000005: 读取位置 0x0000000000000010 时发生访问冲突

如何组装一个注册中心?
随机推荐
Kali view IP address
10 let operator= return a reference to *this
鹏哥C语言20210811程序结构作业
创建EOS账户 Action
Error[Pe147]: declaration is incompatible with '错误问题
使用float实现左中右布局,中间内容自适应
二叉树的遍历 递归+迭代
控制随机抽中几率 [ C# | Random ]
抽象工厂及其改进示例
在神州IV开发板上成功移植STemWin V5.22
Sql Server 数据库之完整性约束
Sql Server 数据库之初学体验
SQL Server 之Sql语句创建数据库
WIRESHARK基础教程以太帧的分析。
剑指Offer(二十):包含min函数的栈
Pengge C language - minesweeping 2021-08-16
[leetcode daily question 2021/8/30]528. Choose randomly by weight [medium]
c结构体中定义的成员指针赋值与结构体指针作为成员函数参数的使用
按二进制数中1的个数分类
Oracle create index