【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 ...
随机推荐
- eclipse怎么导出可执行jar包
在eclpse中找到你要导出的java程序 选中它 单击文件 -->export 在弹出的export对话框中找到 jar File 单击选中-->next 按图示顺序依次 选好你的jav ...
- (转)利用Spring AOP自定义注解解决日志和签名校验
一.需解决的问题 部分API有签名参数(signature),Passport首先对签名进行校验,校验通过才会执行实现方法. 第一种实现方式(Origin):在需要签名校验的接口里写校验的代码,例如: ...
- JavaScript修改CSS属性的实例代码
用原生的javascript修改CSS属性的方法. 用JavaScript修改CSS属性 只有写原生的javascript了. 1.用JS修改标签的 class 属性值: class 属性是在标签 ...
- url的正则表达式
http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
- nats
NATS is a family of open source products that are tightly integrated but can be deployed independent ...
- java 中list进行动态remove处理
java中遍历 list遇到需要动态删除arraylist中的一些元素 的情况 错误的方式 for(int i = 0, len = list.size(); i < len; i++){ if ...
- [one day one question] safari缓存太厉害
问题描述: safari缓存太厉害,这怎么破? 解决方案: window.onpageshow = function(event) { if (event.persisted) { window.lo ...
- [UI基础][实现]九宫格之应用程序管理
[目标] 1.完成下图所示的View,View中的图片.文字数据从app.list文件读出. 2.思考代码哪里可以进行优化. [分析] 1.创建控件 整个View分12个部分,其中包含一个 UIIma ...
- Linux硬盘扩容(非LVM)
环境说明: 虚拟机:Centos6 [root@elements ~]# cat /etc/redhat-release CentOS release 6.10 (Final) [root@eleme ...
- asp.net自定义控件之加载层
知识点:JQuery.Ajax.自定义控件 该文旨在给大家开发自定义控件(结合js)一个思路,一个简单的示例,可能在实际项目中并不会这样做. 先来看看效果: 1.在静态页面里开发好想要的效果 jQue ...