【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 ...
随机推荐
- rest_framework知识总汇
RESTful规范 rest_framework基础 rest_framework基本组件(权限.认证.频率) rest_framework渲染器 rest_framework版本控制 解析器.路由控 ...
- Day19 客户关系系统实战
day19 今日内容 Service事务 客户关系管理系统 Service事务 在Service中使用ThreadLocal来完成事务,为将来学习Spring事务打基础! 1 DAO中的事务 ...
- mysql 数据操作 单表查询 group by 练习
小练习: 1. 查询岗位名以及岗位包含的所有员工名字 mysql> select post,group_concat(name) from employee group by post ; +- ...
- 关于sed -i 修改selinux 的软链接文件的问题
关于sed -i 修改selinux 的软链接文件的问题 http://blog.csdn.net/kumu_linux/article/details/8598005 因为sed -i /etc/s ...
- 查T结果与Z结果的P值[转载]
T检验P值表格来自:https://blog.csdn.net/m0_37777649/article/details/74937242 Z检验表格来自:https://wenku.baidu.com ...
- linux磁盘空间使用问题
linux磁盘空间用满的处理方法 linux下空间满可能有两种情况 可以通过命令 df -h 查看磁盘空间占用,实际上是查看磁盘块占用的文件(block) df -i 查看索引节点的占用(Inod ...
- httpclient接口测试完整用例以及获取信息的方法
原文地址https://blog.csdn.net/fhaohaizi/article/details/78088075 原文地址https://blog.csdn.net/fhaohaizi/art ...
- 特定于类的内存管理---《C++必知必会》 条款36
我们可以量身定制 operator new 和 operator delete 用于某个类类型,而不是必须使用标准版的 operator new 和 operator delete. 注意:我们不可以 ...
- python 字节字符串上的字符串操作
问题:想在字节字符串上执行普通的文本操作(比如移除,搜索和替换). 解决方案 1)字节字符串同样也支持大部分和文本字符串一样的内置操作.比如: >>> data = b'Hello ...
- oracle_多字段统计(多count)
oracle_多字段统计 查询同一张表中同一字段的不同值的综合,方法如下: select o.code 礼品代码, o.name 礼品名称, l.couponactivityid 券活动定义, cou ...