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 ...
随机推荐
- mvc 实体类 自定义验证特性
mvc 实体类验证的时候 如果有多个验证特性需要在属性上层叠很多个验证特性,显得属性特别臃肿并且也不够直观,极大地影响我使用它的兴趣,所以我想自定义一个验证特性,然后将所有需要验证的情形全部放在一个特 ...
- Oracle 环境下 GoldenGate 集成抽取(Integrated Capture)模式与传统抽取模式(Classic Capture)间的切换
检查抽取进程模式 在 GGSCI 环境下,执行类似如下语句查看特定进程的状态. GGSCI> info <Group_Name> 其中,<Group_Name> 为进程名 ...
- 用eclipse+svn插件,上传新项目到svn服务器
给定trunk路径,https://svn.ws.125089.com/public/nlp/3434index/IndexByModelSolr/trunk/. 其中自己的web项目名字是Index ...
- day63-webservice 01.cxf介绍
CXF功能就比较强了.CXF支持soap1.2.CXF和Spring整合的非常密切.它的配置文件基本就是Spring的配置文件了.CXF是要部署在服务器才能用的.CXF得放到Web容器里面去发布.CX ...
- codeforce465DIV2——D. Fafa and Ancient Alphabet
概率的计算答案给出的这张图很清楚了,然后因为要求取模,a/b%M=a*b^-1%M=a*inv(b,M)%M; #include <cstdio> #include <cstring ...
- rsa 数学推论
RSA加密算法是最常用的非对称加密算法,CFCA在证书服务中离不了它.但是有不少新来的同事对它不太了解,恰好看到一本书中作者用实例对它进行了简化 而生动的描述,使得高深的数学理论能够被容易地理解.我们 ...
- Visual Studio + C# + Xamarin = iOS/Android/Windows Apps
Visual Studio 跨平台開發實戰 (1) -- Hello Xamarin! 前言 應用程式發展的腳步,從來沒有停過.從早期的 Windows 應用程式, 到網路時代的 web 應用程式,再 ...
- 面试题:filter过滤器 listener 监听器 案例有点用
1.Filter工作原理(执行流程) 当客户端发出Web资源的请求时,Web服务器根据应用程序配置文件设置的过滤规则进行检查,若客户请求满足过滤规则,则对客户请求/响应进行拦截,对请求头和请求数据进行 ...
- ROS naviagtion analysis: costmap_2d--LayeredCostmap
博客转自:https://blog.csdn.net/u013158492/article/details/50490490 在数据成员中,有两个重要的变量:Costmap2D costmap_和 s ...
- getopt两个模块getopt 和gun_getopt 的异同
getopt的两个模块getopt和gun_getopt都可以接收参数,但是又有不同; 先看 getopt.getopt这个模块: import sys import getopt def main( ...