Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
题目描述
实现atoi函数,将一个字符串转化为数字
测试样例
Input: "42"
Output: 42
Input: " -42"
Output: -42
Input: "4193 with words"
Output: 4193
Input: "words and 987"
Output: 0
详细分析
这道题的corner cases非常多,请务必确保下面cases都能通过的情况下再提交。
"42"
"words and 987"
"-91283472332"
"0-1"
"-000000000000001"
" 0000000000012345678"
"10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000522545459"
"-2147483647"
"-2147483648"
"2147483648"
"2147483649"
""
"7"
" +0 123"
算法实现
class Solution {
public:
string trim(const std::string&str){
string nstr;
int i=0;
while(isspace(str[i])){
i++;
}
for(;i<str.length();i++){
if(isspace(str[i])){
break;
}
nstr +=str[i];
}
return nstr;
}
int myAtoi(string str) {
str = trim(str);
if(str.length()==0 || (str[0]!='+'&&str[0]!='-'&& !isdigit(str[0]))){
return 0;
}
int i=0;
//consume sign char
if(str[0] =='+' || str[0]=='-'){
i++;
}
string nstr;
while(isdigit(str[i])){
nstr+=str[i];
i++;
}
if(nstr.length()==0){
return 0;
}
i=0;
// consume meaningless zeros
while(nstr[i]=='0'){
i++;
}
nstr = nstr.substr(i);
long long result = 0L;
unsigned long long exp = 1;
for(int k=nstr.length()-1;k>=0;k--){
result += ((int)(nstr[k]-'0'))*exp;
if(exp> numeric_limits<int>::max()){
return str[0]=='-'?numeric_limits<int>::min():numeric_limits<int>::max();
}
exp*=10;
if(result> numeric_limits<int>::max()){
return str[0]=='-'?numeric_limits<int>::min():numeric_limits<int>::max();
}
}
return str[0]=='-'?-result:result;
}
};
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)的更多相关文章
- leetcode:String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [LeetCode][Python]String to Integer (atoi)
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...
- 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] 8. String to Integer (atoi) (Medium)
实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...
- 【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)(模拟题,水)
8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...
- [LeetCode] 8. String to Integer (atoi) 字符串转为整数
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
- LeetCode——8. String to Integer (atoi)
一.题目链接:https://leetcode.com/problems/string-to-integer-atoi/ 二.题目大意: 实现一个和C语言里atoi具有相同功能的函数,即能够把字符串转 ...
- 【LeetCode】String to Integer (atoi) 解题报告
这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...
随机推荐
- RTMP_EnableWrite(rtmp)
发布流关键函数: RTMP_EnableWrite(rtmp); 将rtmp设置可写状态,会发出publish指令,否则是play指令:
- EditText中onEditorAction监听事件执行两次
Android的EditText通过setOnEditorActionListener给文本编辑框设置监听事件,但是在其处理方法onEditorAction中的逻辑在每次回车后都触发了两次, 原来是在 ...
- Python基础学习五 内置函数
1.函数补充: 1)函数返回值return可以有多个 2)补充示例: nums = [0,1,2,3,4,5,6,7,8] #如何将list里面的元素变为字符串类型 new_nums = [str(x ...
- Stall Reservations(贪心+优先队列)
Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will onl ...
- C51串口的SCON寄存器及工作…
原文地址:C51串口的SCON寄存器及工作方式作者:batistar 一,串行口控制寄存器SCON 它用于定义串行口的工作方式及实施接收和发送控制.字节地址为98H,其各位定义如下表: D7 D6 D ...
- 2 时间管理和内存管理
时间管理 uC/OS-II的时间管理是通过定时中断来实现的,该定时中断一般为10毫秒或100毫秒发生一次(这个时间片段是OS的作者推荐的,大家可以参考邵贝贝翻译的<嵌入式实时操作系统ucos-I ...
- 解决Maximum execution time of 120 seconds exceeded
在循环开始前加入代码: //设置超时时间 ini_set("max_execution_time",18000); set_time_limit(0); set_time_limi ...
- inux下安装ab
1,APR 下载地址:http://apr.apache.org/download.cgi 1)tar -zxf apr-1.4.5.tar.gz ./configure --prefix=/u ...
- Java 基于quartz实现定时 之二(XML方式配置)
<!-- 在spring核心配置文件中进行如下配置 --> <!-- Spring基于quartz定时任务 --> <bean id="triggerByBea ...
- Hadoop完全分别式环境搭建
为学习大数据,需搭建Hadoop大数据环境,在此记录,以备以后查阅,同时分享出来,供需要者参考. 这里分几部分进行整理. 提纲: 一.说明和准备 二.设置免密登陆 分段网址:https://www.c ...