//此题是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. css Hack,用IE11模拟测试的,条件注释要找真IE去测,模拟的无效

    <!DOCTYPE html> <!--[if lt IE 7 ]> <html class="ie6 ie"> <![endif]--& ...

  2. 正则匹配ab不匹配aab

    var a = "aababaabababababaab"; a.replace(/(^|[^a])(ab)+/g, "$1"); var a = " ...

  3. ADO.Net的小知识(连接数据库)

    数据库连接分为两种,分别是断开式连接和打开式连接.下面分别和大家分享一下断开时连接的查询: (1)引入命名空间:using System.Data.SqlClient; 该语句用于导入和ADO.Net ...

  4. Linux服务器的常用备份方法

    系统备份是系统管理工作中十分重要的一个环切,本文详细介绍了各种Linux系统的备份方法,相信对大家的日常管理工作有所帮助. 备份是一项重要的工作,但是很多人没有去做.一旦由于使用不当造成数据丢失,备份 ...

  5. c++ (P262—P277) STL

    STL的六大组件 容器(Container),是一种数据结构,如list,vector,和deques ,以模板类的方法提供.为了访问容器中的数据,可以使用由容器类输出的迭代器: 迭代器(Iterat ...

  6. 包括后台的Android美食APP项目开源代码

    项目简介 小食光定位为一款集美食,社交,LBS服务于一体的美食推荐APP.为你发现周边美食的同时提供一个吃货分享的平台. APP截图     功能模块 美食推荐 :提供基础的美食信息查询: 商家推荐  ...

  7. Android之HTTP网络通信--GET传递

    说明 在做一个项目的时候难免会与服务器打交道,这里我就做一个小的Demo来简单的说明一下HTTP的使用,我这里使用的是图灵的接口,你也可以登陆www.tuling123.com进行申请.我使用的是上面 ...

  8. 微信公众平台接口API

    <?php /** * Author: helen * CreateTime: 2015/12/9 20:14 * description: 微信公众平台接口API */ class Wecha ...

  9. Rshare Pro是否可以放入至客户企业App Store?

    现在很多客户内部部署了苹果授权的企业内部的AppStore,我们的Rshare Pro 是完全允许放入企业搭建的AppStore平台中.但每份需要收费20美元,换成人民币是120元.

  10. JDK Tools - wsimport: 编译 WSDL 生成 JAX-WS 规范的 Java 类

    wsimport 命令是用来编译 wsdl 生成JAX-WS 规范的 Java 类的工具. 命令格式 wsimport [ options ] wsdl 命令参数 -s directory 指定源文件 ...