//此题是easy题,比较简单,主要困难在考虑全输入的各种情况:
//1、开始的时候有空格等空白字符
//2、开头有加减号
//3、溢出(第一次写就是没有考虑到这个情况) //C代码
int myAtoi(char* str) {
int i=;
double result = ;
int IsNegative = ; while(isspace(str[i]))
{
i++;
} if(str[i] == '-')
{
IsNegative = ;
i++;
}
else if(str[i] == '+')
{
IsNegative = ;
i++;
}
else
{ } for(;i<strlen(str);i++)
{
if(isdigit(str[i]))
{
result = result* + (str[i] - '');
}
else
{
break;
}
} if(IsNegative == )
{
result *= (-);
} if(result > INT_MAX)
{
result = INT_MAX;
} if(result < INT_MIN)
{
result = INT_MIN;
} return (int)result; }
#python代码
class Solution(object):
def myAtoi(self, str):
"""
:type str: str
:rtype: int
"""
num = ""
INT_MAX = 2147483647
INT_MIN = -2147483648
i = 0
sum = 0
flag = 1 str = str.strip()
if len(str) == 0:
return 0
else:
if str[i] == '-':
flag = -1
i+= 1
elif str[i] == '+':
flag = 1
i+= 1 if i >= len(str):
return 0 while i<len(str) and str[i].isdigit():
tempint = int(str[i])
if INT_MAX/10 >= sum:
sum = sum*10
else:
if flag == 1:
return INT_MAX
if flag == -1:
return INT_MIN if INT_MAX - tempint >= sum:
sum += tempint
else:
if flag == 1:
return INT_MAX
if flag == -1:
return INT_MIN i+=1 return sum*flag

leetcode 解题 String to Integer (atoi)(C&python)的更多相关文章

  1. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

  2. leetcode day6 -- String to Integer (atoi) &amp;&amp; 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 ...

  3. 【leetcode】String to Integer (atoi)

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  4. [leetcode] 8. String to Integer (atoi) (Medium)

    实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...

  5. Leetcode 8. String to Integer (atoi)(模拟题,水)

    8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...

  6. 【LeetCode】String to Integer (atoi) 解题报告

    这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...

  7. 【JAVA、C++】 LeetCode 008 String to Integer (atoi)

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

  8. Java [leetcode 8] String to Integer (atoi)

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

  9. 蜗牛慢慢爬 LeetCode 8. String to Integer (atoi) [Difficulty: Medium]

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

随机推荐

  1. 101个直接可以拿来用的JavaScript实用功能代码片段(转)

    1.原生JavaScript实现字符串长度截取 function cutstr(str, len) { var temp; var icount = 0; var patrn = /[^x00-xff ...

  2. nginx rewrite 参数和例子

    http://www.cnblogs.com/analyzer/articles/1377684.html ] 本位转自:http://blog.c1gstudio.com/archives/434 ...

  3. HTTPS证书制作

    openssl 安装可参照之前文章 1.mkdir /etc/ssl/xip.ioopenssl genrsa -out /etc/ssl/xip.io/xip.io.key 1024openssl ...

  4. debian 颜色设置

    root@debian:/# vi /root/.bashrc #export PS1='\h:\w\$ 'export PS1='\[\033[1;32;40m\]\u@\h:\w\$ \[\033 ...

  5. [转载]“java.sql.SQLException:指定了无效的 Oracle URL”

    原文地址:"java.sql.SQLException:指定了无效的 Oracle URL"作者:康愚 昨天晚上用MyEclipse连接Oracle,出现了" java. ...

  6. javascript开发中的封装模式(转)

    var bgAuido={ audio : pingfan.$$('audio'), audioBtn : pingfan.$$('audioBtn'), init : function(){ var ...

  7. 深入解析hasOwnProperty与isPrototypeOf

    这里采用一个实例来说明: function Person(name) { //以下都是Person的OwnProperty this.name = name; this.showMe = functi ...

  8. php的传值和传址

    有些情况下,可能希望在函数体内对参数的修改在函数体外也能反映; 使用引用传递参数要在参数前加上&符号; 例子: <?php $a=5; function show(&$a){   ...

  9. h2database源码浅析:锁与MVCC

    Table Level Locking The database allows multiple concurrent connections to the same database. To mak ...

  10. 第八篇、UITableView常用功能(左滑出现多个按钮,多选删除等)

    1.左滑动出现多个按钮 /** * 只要实现了这个方法,左滑出现按钮的功能就有了 (一旦左滑出现了N个按钮,tableView就进入了编辑模式, tableView.editing = YES) */ ...