#-*- coding: UTF-8 -*-
#需要考虑多种情况
#以下几种是可以返回的数值
#1、以0开头的字符串,如01201215
#2、以正负号开头的字符串,如‘+121215’;‘-1215489’
#3、1和2和空格混合形式【顺序只能是正负号-0,空格位置可以随意】的:‘+00121515’
#4、正数小于2147483647,负数大于-2147483648的数字
#其他的情况都是返回0,因此在判断 是把上述可能出现的情况列出来,其他的返回0
#AC源码如下
class Solution(object):
    def myAtoi(self, str):
        """
        :type str: str
        :rtype: int
        """
        if str=="":return 0
        strl=[]
        count=0
        flag=0
        str=str.strip()
        for  c in str:
            if c>='1' and c<='9':
                count+=1
                strl.append(c)
            elif c=='0' and count!=0:
                count+=1
                strl.append(c)
            elif c=='-' and flag==0:
                flag+=1
                strl.append(c)
            elif c=='+' and flag==0:
                flag+=1
                strl.append(c)
            elif c=='0':continue
            else:break
        str=''.join(strl)
        if str=="":return 0
       
        if str[0]!='-'and str[0]!='+'  :return int(str) if int(str)<2147483647 else 2147483647
        elif str[0]=='-' and len(str)>1 :return -int(str[1:]) if int(str[1:])<2147483648 else -2147483648
        elif str[0]=='+' and len(str)>1 :return  int(str[1:]) if int(str[1:])<2147483647 else 2147483647
        return 0
            
sol=Solution()
print sol.myAtoi("-2147483648")

【leetcode❤python】 8. String to Integer (atoi)的更多相关文章

  1. 【leetcode❤python】13. Roman to Integer

    #-*- coding: UTF-8 -*-#从前向后遍历罗马数字,#如果某个数比前一个数小,则加上该数.反之,减去前一个数的两倍然后加上该数###-----技术规则-----#----------- ...

  2. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...

  3. leetcode第八题 String to Integer (atoi) (java)

    String to Integer (atoi) time=272ms   accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...

  4. 【一天一道LeetCode】#8. String to Integer (atoi)

    一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...

  5. 【leetcode❤python】Sum Of Two Number

    #-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...

  6. 【LeetCode】8. String to Integer (atoi) 字符串转换整数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...

  7. 【LeetCode】8. String to Integer (atoi) 字符串转整数

    题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  8. 【leetcode】8. String to Integer (atoi)

    题目描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...

  9. 【LeetCode】008. String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

随机推荐

  1. oracle的索引维护

    索引重建 Alter index idx_name rebuild partition index_partition_name [online nologging] 需要对每个分区索引做rebuil ...

  2. 夺命雷公狗---node.js---14之DNS

    node下如果想域名解析是需要通过apache或者ng的反向版定才可以实现的,但是他也给我们留下了哟套DNS操作方法: /** * Created by leigood on 2016/8/30. * ...

  3. sql语句删除数据表重复字段的方法

    大家都可能遇到字段重复的情况,网上很多人在找方法,也给出了一些方法,但是有的方法是误导大家,铁牛写出以下方法,方便大家使用 1.通过group by把重复的字段筛选出来,并建立临时表tmp 1 cre ...

  4. 慎用MySQL replace语句

    语法: REPLACE [LOW_PRIORITY | DELAYED] [INTO] tbl_name [PARTITION (partition_name,...)] [(col_name,... ...

  5. Intel Edison

    起步: https://software.intel.com/zh-cn/node/628224 刷机: https://software.intel.com/zh-cn/flashing-firmw ...

  6. Spring JDBC保存枚举对象含关键字报错原因之一

    报错信息: org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized S ...

  7. IIS7.5真变态,服务器时间格式导致不生成WebResource.axd

    把时间调成HH:mm格式后,IIS不生成WebResource.axd了,,,从凌晨一点弄到现在......

  8. C# Community Projects

    Community Supported C# Drivers See the officially supported MongoDB C# driver mongodb-csharp driver ...

  9. Linux下后台程序完成自动输入密码等交互行为的例子

    今天要开发一个定时任务,然后加入cron列表中.但是有个问题摆在眼前,脚本的执行中需要输入数据库密码: mysql -u root -p << SQL use db; set names  ...

  10. C#:关联程序和文件

    一.关联代码 /// <summary> /// 关联程序和类型 /// </summary> private void RegFileExt() { try { string ...