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 ...
随机推荐
- cocos2dx 公告效果
第一种方法: http://blog.csdn.net/jackystudio/article/details/12991977 [玩转cocos2d-x之十六]滚动字幕和公告 第二种方法: http ...
- SQL 学习与工作日常:语句积累
1.跨服务器连接数据表 --打开服务器配置'Ad Hoc Distributed Queries' --exec sp_configure 'show advanced options',1 --re ...
- Java栈实现
栈数组实现一:优点:入栈和出栈速度快,缺点:长度有限(有时候这也不能算是个缺点) public class Stack { private int top = -1; private Object[] ...
- 寻找对象在父元素下的index
方法一. window.onload=function(){ //寻找对象在父元素下的index function getIndexParent(element){ var ...
- 在 Ubuntu 16.04 中安装谷歌 Chrome 浏览器
进入 Ubuntu 16.04 桌面,按下 Ctrl + Alt + t 键盘组合键,启动终端. 也可以按下 Win 键(或叫 Super 键),在 Dash 的搜索框中输入 terminal 或&q ...
- CENTOS install summary
1 centos6.5 before version : first :ultraISO write into u disk,second: copy source iso file into u d ...
- C#学习笔记13:静态方法、方法重载和ref、out参数
静态方法 调用:如果你写的方法和Main()方法在同一个类中,直接写方法名. 如果不在一个类中,需要类名.方法名(); 非静态方法: 调用:创建一个类的对象 对象名.方法名(); Person pe ...
- C#常量字段
const 常量字段使用方法 using System;using System.Collections.Generic;using System.Linq;using System.Text;usi ...
- 杂技之sharpdevelop调试aps.net
背景: 本人笔记本电脑不给力,vs打开实在太慢,因此考虑使用sharpdevelop,但sharpdevelop有点麻烦事,就是不支持asp.net的调试,为解决此问题,本人在此杂技一把了 方案一: ...
- TextView实现跑马灯效果
网上有很多跑马灯的介绍,有很多跑马灯的代码.或许我的不是最好的,但是应该很容易明白的. 我们先来介绍一个跑马灯的代码 <LinearLayout xmlns:android="http ...