当前位置:网站首页>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
边栏推荐
- C语言命名空间的定义与使用
- Flutter TextField怎样去除下划线及有焦点时颜色
- The problem of formatting IAR sprintf floating point to 0.0 in UCOS assembly
- RT thread learning notes (VIII) -- start the elmfat file system based on SPI flash (Part 2)
- RT thread learning notes (VII) -- open the elmfat file system based on SPI flash (middle)
- Common classes (understand)
- [leetcode daily question 2021/2/14]765. Lovers hold hands
- 鹏哥C语言第六节课
- 点击el-dropdown-item/@click.native
- 10 let operator= return a reference to *this
猜你喜欢

从蚂蚁的觅食过程看团队研发(转载)

解决:无法加载文件 C:\Users\user\AppData\Roaming\npm\npx.ps1,因为在此系统上禁止运行脚本 。

2021-08-12函数递归_和鹏哥学习C语言

mysql20210906

抽象工厂及其改进示例

RT thread learning notes (III) -- building a compilation environment with scons

使用Selenium抓取zabbix性能监控图

Issue 7: how do you choose between curling up and lying flat

mysql20210906
![[leetcode daily question 2021/8/31] 1109. Flight reservation statistics [medium] differential array](/img/9d/5ce5d4144a9edc3891147290e360d8.png)
[leetcode daily question 2021/8/31] 1109. Flight reservation statistics [medium] differential array
随机推荐
软件测试综述之软件测试的背景、实质、软件开发的过程
从蚂蚁的觅食过程看团队研发(转载)
c 语言中宏参数的字符串化跟宏参数的连接
Happens-Before原则深入解读
toolstrip 去边框
RT thread learning notes (I) -- configure RT thread development environment
242.有效的字母异位词
剑指Offer(五十二):正则化表达式
解决:无法加载文件 C:\Users\user\AppData\Roaming\npm\npx.ps1,因为在此系统上禁止运行脚本 。
344.反转字符串
扫雷pro版2021-08-19
IAR sprintf 浮点 在UCOS 总格式化成0.0的问题
349. 两个数组的交集
一文详解Nodejs中fs文件模块与path路径模块
[machine learning notes] [face recognition] deeplearning ai course4 4th week programming
20210807 1 c language program structure
访问权限——private,public,protected
C语言命名空间的定义与使用
智能合约dapp系统开发流程技术
构建ARM嵌入式开发环境