【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 ...
随机推荐
- MySQL中数据表的查操作
查询数据表的全部内容 mysql> show tables;#查看当前数据库下的全部表 +--------------------+ | Tables_in_ceshi_ku | +------ ...
- SaltStack系列(一)之环境部署、命令及配置文件详解
一.SaltStack介绍 1.1 saltstack简介: saltstack是基于python开发的一套C/S架构配置管理工具,它的底层使用ZeroMQ消息队列pub/sub方式通信,使用SSL证 ...
- Django model中数据批量导入bulk_create()
在Django中需要向数据库中插入多条数据(list).使用如下方法,每次save()的时候都会访问一次数据库.导致性能问题: for i in resultlist: p = Account(nam ...
- Jitamin
安装环境要求 PHP 5.6或更高(推荐使用PHP7) 数据库, 推荐使用MySQL 或 PostgreSQL. 当然SQLite也可以运行. Composer 安装手册 一. 克隆代码 假设我们把j ...
- weiwo.wxmmd.com将您重定向的次数过多。尝试清除 Cookie.
折腾了很久,最后更换PHP版本解决了,我的项目用的tp3.1.2,出现上图问题时的php版本是7.1,换回5.6就没有这个问题.希望能为大家提供一个思路.
- 数据挖掘-逻辑Logistic回归
逻辑回归的基本过程:a建立回归或者分类模型--->b 建立代价函数 ---> c 优化方法迭代求出最优的模型参数 --->d 验证求解模型的好坏. 1.逻辑回归模型: 逻辑回归(L ...
- The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online Solution
A Live Love 水. #include<bits/stdc++.h> using namespace std; typedef long long ll; ; const i ...
- python: 随机选择
想从一个序列中随机抽取若干元素,或者想生成几个随机数. random 模块有大量的函数用来产生随机数和随机选择元素.比如,要想从一个序列中随机的抽取一个元素,可以使用random.choice() : ...
- linux history记录格式修改
#保存一万条命令记录 sed -i 's/^HISTSIZE=1000/HISTSIZE=10000/g' /etc/profile #在/etc/profile的文件尾部添加如下行数配置信息 ### ...
- 20145328 《网络对抗技术》逆向及Bof基础实践
20145328 <网络对抗技术>逆向及Bof基础实践 实践内容 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回 ...