【Leet Code】String to Integer (atoi) ——常考类型题
String to Integer (atoi)
Total Accepted: 15482 Total
Submissions: 106043My Submissions
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
字符串的操作。敲代码常常性遇到。string这个类真的很实用哟,题目要求自行实现atoi的功能:
class Solution
{
public:
int atoi(const char *str)
{
while(' ' == *str)
{
str++;
}
bool isNegative = false;
if('-' == *str)
{
isNegative = true;
str++;
}
else if('+' == *str)
{
str++;
}
long long ret = 0;
while(*str)
{
if( isdigit(*str) )
{
ret = ret * 10 + (*str - '0');
if(isNegative && (-ret <= INT_MIN))
{
return INT_MIN;
}
if(!isNegative && (ret >= INT_MAX))
{
return INT_MAX;
}
}
else
{
break;
}
str++;
}
return (isNegative ? -ret : ret);
}
};
【Leet Code】String to Integer (atoi) ——常考类型题的更多相关文章
- LeetCode【8】. String to Integer (atoi) --java实现
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode第八题 String to Integer (atoi) (java)
String to Integer (atoi) time=272ms accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...
- 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 ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode-algorithms-8 String to Integer (atoi)
leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...
随机推荐
- BZOJ1019 汉诺塔
定义f[i][j]为将i柱上的j个盘挪走(按优先级)的步数 p[i][j]为将i柱上的j个盘按优先级最先挪至何处 首先考虑一定p[i][j]!=i 设初始为a柱,p[i][j-1]为b柱 考虑两种情况 ...
- php -- php读取sqlserver中的datetime出现的格式问题
php连接sqlserver2005时,读取出来的数据是01 15 2014 12:00AM, 也就是说日期的格式是MM DD YY hh:mmAM 那如何把它转变成24小时制,且显示的格式为YY-M ...
- HashMap结构及使用
HashMap的数据结构 HashMap主要是用数组来存储数据的,我们都知道它会对key进行哈希运算,哈系运算会有重复的哈希值,对于哈希值的冲突,HashMap采用链表来解决的.在HashMap里有这 ...
- 配置Maven环境变量与Intelij IDE配置Maven
Maven有什么用? 以前我们导入第三方jar包的流程是什么?一般是download,然后copy到项目中,然后依赖(library)项目,最后被我们使用. 通俗的说,就是不用我们自己去downloa ...
- Highcharts构建分组分类坐标轴
Highcharts构建分组分类坐标轴 分组分类坐标轴是将坐标轴的类别标签进行进一步分组,从而形成双层.多层结构. 这样更利于数据分组展现. 实现分组分类坐标轴须要借助第三方插件Grouped-Cat ...
- 菜鸟学Java(二十)——你知道long和Long有什么差别吗?
Java中数据类型分两种: 1.基本类型:long,int,byte,float,double2.对象类型:Long,Integer,Byte,Float,Double其他一切java提供的,或者你自 ...
- win10怎么彻底关闭自动更新
原文:https://jingyan.baidu.com/article/9faa7231e7b78b473c28cbb6.html 方法/步骤 1 单击左下角开始菜单点击设置图标进入设置界面 2 ...
- 基于Memcached的tomcat集群session共享所用的jar及多个tomcat各种序列化策略配置
原文:http://www.cnblogs.com/interdrp/p/4096466.html 多个tomcat各种序列化策略配置如下:一.java默认序列化tomcat配置conf/contex ...
- Unity3D如何减少安装包大小
译官方文档:http://docs.unity3d.com/Manual/ReducingFilesize.html PDF文档:http://www.rukawa.cn/Uploads/Attach ...
- Interactive Messager
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...