【LeetCode 8_字符串_实现】String to Integer (atoi)

enum status{VALID = , INVALID};
int g_status;
long long SubStrToInt(const char* str, bool minus)
{
long long num = ;
int flag = minus ? - : ;
while (*str != '\0') {
if (*str >= '' && *str <= '') {
num = num * + flag * (*str - '');
if ((!minus && num > 0x7FFFFFFF)
|| (minus && num < (signed int)0x80000000)) {
num = ;
break;
}
str++;
} else {
num = ;
break;
}
}
if (*str == '\0')
g_status = VALID;
return num;
}
int StrToInt(const char* str)
{
g_status = INVALID;
long long num = ;
bool minus = false;
if (str != NULL && *str != '\0') {
if (*str == '+') {
str++;
}
else if (*str == '-') {
minus = true;
str++;
}
if (*str != '\0')
num = SubStrToInt(str, minus);
}
return (int)num;
}
需要考虑的几个方面:
1、输入的字符串表示正数、负数和0;
2、边界值,最大的正整数和最小的负整数;
3、输入字符串为NULL、空字符串、字符串中有非数字、字符串开始的一段有空格等。
【LeetCode 8_字符串_实现】String to Integer (atoi)的更多相关文章
- 【LeetCode每天一题】String to Integer (atoi)(字符串转换成数字)
Implement atoi which converts a string to an integer.The function first discards as many whitespace ...
- 【Leetcode】【Easy】String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 【leetcode刷题笔记】String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))
8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...
- 【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 字符 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
随机推荐
- scrapy框架(2)
一.使用scrapy框架发送post请求 1.需求一:使用scrapy发送百度翻译中的ajax请求 创建一个项目,如下目录,修改settings.py文件中的 "ROBOTSTXT_OBEY ...
- Navicat运行sql文件报错out of memory
下载并安装mysql workbench:
- 【抓包】火狐浏览器F12
页面请求服务器的get.post两种请求,还有其他种,但是其他中基本不用,所以只记住get和post两种请求方法即可. 1.get(当前页面向服务器传值--即请求服务器)---弊端--传值长度有限 F ...
- MySQL server has gone away 问题的解决方法(转)
mysql出现ERROR : (2006, 'MySQL server has gone away') 的问题意思就是指client和MySQL server之间的链接断开了. 造成这样的原因一般是s ...
- PAT 1128 N Queens Puzzle[对角线判断]
1128 N Queens Puzzle(20 分) The "eight queens puzzle" is the problem of placing eight chess ...
- 新版samba安装过程
yum install samba samba-client samba-swat cp -p /etc/samba/smb.conf /etc/samba/smb.conf.orig /bin ...
- Python: 二进制、八进制、十六进制转换或者输出
为了将整数转换为二进制.八进制或十六进制的文本串,可以分别使用bin() ,oct() 或hex() 函数: >>> x = 1234 >>> bin(x) '0b ...
- redis入门学习记录(一)
1.linux在线下载Redis ,官网地址:https://redis.io/download目前,最新的Redist版本为redis-5.0.0,使用wget下载 进入/usr/local/src ...
- RESTful源码笔记之RESTful Framework的APIview, Viewset总结分析
0x00 引言 官方文档:http://www.django-rest-framework.org/drf为我们提供强大的通用view的功能,本博客对这些view进行简要的总结分析.首先,我们看一下 ...
- oracle 11g 数据库中报:协议适配器错误
本人遇到该问题,到数据库服务器上重新启动监听和实例就OK了.