LeetCode OJ String to Integer (atoi) 字符串转数字
#include <iostream>
#include <assert.h>
using namespace std;
int ato(const char *str) {
int i=,e=,s=;
int max=,min=-;
int f=;
int tem[]={};
unsigned int pan=;
while(*str==' '){ //过滤掉连续空格
str++;
}
if(*str=='-'||*str=='+'||(*str<=''&&*str>='')){
if(*str=='-'||*str=='+'){ //过滤掉正负号
if(*str=='-')
f=-;
str++;
}
while(*str=='') //过滤掉前面的无用的0
str++;
if( *str<=''&&*str>='' ){ //过滤掉非数字的字符 while( *str!='\0'){ //判断字符串是否结束
if( *str>''||*str<'' ) //字符串还没结束,就出现非数字字符
break;
if( i== ){ //大于10位的数字都要作溢出处理
if(f==-)
return min;
else
return max;
}
tem[i++]=*str-'';
e=e*+(*str-'');
str++;
}
if(i==){ //刚好10位,需要特殊处理
if(f==-){ //负数处理
if(tem[]>){ //超过30,0000,0000
return min;
}
for(i=;i<;i++){
pan=pan*+tem[i];
}
if(pan>=) // 负数的绝对值大于等于2147483648
return min;
}
else{ //正数处理
if(tem[]>){
return max;
}
for(i=;i<;i++){
pan=pan*+tem[i];
}
if(pan>=) // 负数的绝对值大于等于2147483648
return max;
}
}
return f*e;
}
else
return ;
}
return ;
}
int main(){
char str[];
int qq=;
while(cin>>str){
qq=ato(str);
cout<<qq<<endl;
}
return ;
}
这个东西原来挺难考虑周全的,还好leetcode会指出是输入什么数据会输出错误,不然估计找一辈子了。
鉴于这么苛刻的条件,贴出自己的不简洁的代码,这道题第一次AC。
以后有好办法再修改。
LeetCode OJ String to Integer (atoi) 字符串转数字的更多相关文章
- [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)字符串转整数
Implement atoi which converts a string to an integer. The function first discards as many whitespace ...
- 【LeetCode】String to Integer (atoi)(字符串转换整数 (atoi))
这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...
- Leetcode 8 String to Integer (atoi) 字符串处理
题意:将字符串转化成数字. 前置有空格,同时有正负号,数字有可能会溢出,这里用long long解决(leetcode用的是g++编译器),这题还是很有难度的. class Solution { pu ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- 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 字符 ...
- [leetcode] 8. String to Integer (atoi) (Medium)
实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...
随机推荐
- jquery用Ajax中的回调函数时注意事项
前端代码 <script language="javascript" type="text/javascript" src="<?php ...
- 如何在win7下配置IIS?
- 使用PL/SQL连接远程的Oracle数据库
PL/SQL不仅可以连接本机的oracle数据库.也可以连接远程的数据库. 需要修改一个文件:在本机oracle 数据库的安装目录下找到这个文件: /oracle/ora92/network/admi ...
- 怎么设置 mysql 多主复制
更新 其实本文主要来自www.digitalocean.com ,但是我没有买他们家的 VPS 用来 demo 了.只是用vagrant 来模拟了. 介绍 说说关于通过两台 vps 来扩展 mysql ...
- 分析与提取QQ木马盗号技术
程序大致的流程如下图: 因为是用画图工具画的,所以大家就将就看下把,有什么不对的地方请多多指教: 程序是用Delphi写的,只有加载器加了个upx壳,其他的都没有加壳:所以分析起来就比较简单了: 这个 ...
- UVa 11426 - GCD - Extreme (II)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- Android在Eclipse上的环境配置
哈哈,首先转一个:https://my.oschina.net/fusxian/blog/293935 谢谢分享者 1.android SDK安装出现Failed to fetch URL http: ...
- nginx日志切割
脚本完成后将其存入 Nginx 安装目录的 sbin 中,取名为 cut-log.sh,之后使用 crontab -e 新增一个定时任务,在其中增加执行这个脚本: #!/bin/bash ## 零点执 ...
- C++-const_cast只能用于指针和引用,对象的const到非const可以用static_cast
Static_cast可以对对象也可以对指针也可以对引用,但是const_cast只可以对指针和引用使用,后者不可以对对象用,如果你要把一个const值转化为非const值只能用隐式执行或通过使用st ...
- POJ 1845 求a^b的约数和
题目大意就是给定a和b,求a^b的约数和 f(n) = sigma(d) [d|n] 这个学过莫比乌斯反演之后很容易看出这是一个积性函数 那么f(a*b) = f(a)*f(b) (gcd(a,b) ...