leetcode 解题 String to Integer (atoi)(C&python)
//此题是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)的更多相关文章
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- 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 ...
- 【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) (Medium)
实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...
- Leetcode 8. String to Integer (atoi)(模拟题,水)
8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...
- 【LeetCode】String to Integer (atoi) 解题报告
这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...
- 【JAVA、C++】 LeetCode 008 String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- Java [leetcode 8] String to Integer (atoi)
问题描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...
- 蜗牛慢慢爬 LeetCode 8. String to Integer (atoi) [Difficulty: Medium]
题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cas ...
随机推荐
- 《RESTful Web Services》第一章 使用统一接口
序言 HTTP是一种应用层协议.SOAP和一些Ajax Web框架都将HTTP作为一种传输信息的协议,难以充分利用HTTP层的基础设施. 1.2 如何保持交互的可见性 可见性是HTTP的一个核 ...
- Win7无线网络共享设置方法
http://jingyan.baidu.com/article/4f34706e89bb2ae387b56d0b.html
- C# 之 遍历本地文件夹下的所有文件
/// <summary> /// 遍历 rootdir目录下的所有文件 /// </summary> /// <param name="rootdir&quo ...
- MySQL(18):Select- subquery子查询
1. Select- subquery子查询 子查询:是将一条查询语句嵌套在另一条查询语句之中. 2. 案例 需求:查询获得代课天数最多的那个老师的信息. 思路:先获得最多的代课天数是多少天,然后再判 ...
- c++与c不太相同的一些地方2
1.一般全局变量放到cpp 文件里面,而不是.h 里面.要不然容易乱套,这个乱几次就好了,先记住这样一种编码规范. 以为大家都引入就比较麻烦,但是实现起来就只有cpp里面才有.毕竟.h是要被包含的文件 ...
- sql基本语法:
1.create database db_name; --创建数据库 2.drop database db_name; --删除数据库 3.show create database db_name\ ...
- posix thread互斥量
互斥量 互斥量(Mutex)是“mutual exclusion”的缩写.互斥量是实现线程同步,和保护同时写共享数据的主要方法.使用互斥量的典型顺序如下:1. 创建和初始一个互斥量 2. 多个线程尝试 ...
- [YII]将ar查询结果转化成数组
$Column= Column::model()->findAll(); $data = array_map(create_function('$m', 'return $m->getAt ...
- MongoDB自定义函数部分 定义及引用
1. //定义一个Sum的函数 db.system.js.save({_id:"Sum", value:function(key,values) { ; ;i <values ...
- 301页面转向 php
新建301.php页面,在程序入口文件index.php引用301.php页面 301.php内容如下,仅用于参考: <?php$the_host = $_SERVER['HTTP_HOST'] ...