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 ...
随机推荐
- 94. Binary Tree Inorder Traversal (Java)
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- 获取图书isbn信息 共享图书开发 图书信息接口开发过程中的心得体会
最近做一个图书共享的项目,需要用户扫一扫书籍后面的一维码,获取到书籍的isbn号码,然后通过这个isbn号码能够直接获取到这本书的名字.简介.价格.图片等信息. 于是百度搜了下,之前很多的豆瓣的接口, ...
- 【Groovy】 Groovy笔记
一.简单了解Groovy Groovy简介: Groovy是基于JVM的敏捷开发语言,语法与Java类似,但更加简洁,容错性也比Java强,同时Java能非常好的契合(例如Groovy能够使用Java ...
- IIS7发布asp.net mvc提示404
之前服务器用的都是2003Server的服务器,发布mvc项目都没问题,今天换了一台机器,系统为Windows Server2008 R2 64位的发布mvc项目后就提示: 百度看到好多人说在web ...
- 一、Nginx多站点配置
一.下载 目录文件: 二.运行方式 (1)直接双击nginx.exe,双击后一个黑色的弹窗一闪而过 (2)打开cmd命令窗口,切换到nginx解压目录下,输入命令 nginx.exe 或者 start ...
- Codeforces Round #581 (Div. 2)A BowWow and the Timetable (思维)
A. BowWow and the Timetable time limit per test1 second memory limit per test256 megabytes inputstan ...
- pidstat 命令详解(转载)
转自https://www.jianshu.com/p/3991c0dba094 pidstat 概述 pidstat是sysstat工具的一个命令,用于监控全部或指定进程的cpu.内存.线程.设备I ...
- python file对象测试数据的读写操作及OS模块介绍(四)
import from....import 引入模块 引入类 ①import 如果文件在lib下而且是python模块 :import 模块名. ②from....import from 包名.包 ...
- 二进制数组-ArrayBuffer对象
ArrayBuffer对象:存储二进制数据的一段内存,不能写/读 ,类似数组的对象 只能通过TypedArray视图/DataView视图 读/写 va buf = new ArrayBuffer(3 ...
- Attention机制中权重的计算
Attention mechanism中,给输入序列中对应的每一个Ht分配权重(打分)究竟是如何打分? 输入序列打分,a(s, h) 其中s是输出序列的t-1时刻的隐藏层状态,h是输入的多个状态,