//此题是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. python的内存管理机制 图解+Django Web开发学习笔记

    http://www.cnblogs.com/CBDoctor/p/3781078.html http://www.cnblogs.com/vamei/p/3232088.html http://bl ...

  2. 原 Debian设置开机自动启动与关闭

    发表于1年前(2013-01-08 13:01)   阅读(2380) | 评论(0) 2人收藏此文章, 我要收藏 赞0 开机自动启动 update-rc.d chkconfig 熟悉debian系统 ...

  3. [COCOS2DX]COCOS命令新建项目+编译安卓项目并成功运行

    全程搭建过程参考网址: http://blog.csdn.net/lengxue789/article/details/38116475 http://blog.csdn.net/cbbbc/arti ...

  4. 【转】使用Beaglebone Black的I2C (二)——使用C语言和i2c-dev驱动

    在本博客的<使用Beaglebone Black的I2C(一)>中,介绍了BBB上无需编程对i2c总线进行读写操作的方法,本文将介绍如何在c语言程序中使用i2c-dev驱动来操作i2c设备 ...

  5. js数组求和

    array1.reduce(callbackfn[, initialValue]) callback : 函数执行在数组中每个值 initialValue : 对象作为第一个参数回调的第一次调用使用 ...

  6. JavaScript 零散知识点1 (正则表达式+定时器+hover)

    1.clear:both清楚浮动影响//css中 2.正则表达式 search方法 :指明是否存在相应的匹配,如找到一个返回一个整数值,表明这个匹配距离字符串开始的偏移位置,如果没有找到匹配返回-1f ...

  7. HTML5吧!少年

     一.为了能使IE9以下的IE浏览器也能支持html5的标签,所以首先得在文档头部用条件注释的方法引入那段著名的代码. 1 2 3 <!--[if lt IE 9]> <script ...

  8. c# 远程监控(2) 摄像头调研及模拟

    经过N多调研,最终选择了OpenCV(Emgu CV) ** 至于DirectShow, OpenCV等等其他大家可以百度,在这里我就不再赘述 环境:vs2010 vs2012 vs2013均可 Op ...

  9. 第一个Servlet

    一,第一个Servlet的编写过程 1,建立JavaWeb应用目录 HelloServlet--web应用名称 classes:Servlet就放在此处 lib web.xml 2,classes目录 ...

  10. SpringMVC使用@ResponseBody注解返回中文字符串乱码的问题

    先说一下我的经历,以及解决问题的而过程. 在使用SpringMVC的时候,最开始的时候在配置文件中使用<mvc:annotation-driven />去自动注册DefaultAnnota ...