LeetCode8——String to Integer (atoi)(自己编写atoi函数)
题目:
参考解法:
I think we only need to handle four cases:
discards all leading whitespaces
sign of the number
overflow
invalid input
int myAtoi(char* str)
{int sign = 1, base = 0, i = 0;while (str[i] == ' ') { i++; }//去掉空格if (str[i] == '-' || str[i] == '+') {sign = 1 - 2 * (str[i++] == '-');//这里i进行了++,注意符号也要输出的 }//判断符号while (str[i] >= '0' && str[i] <= '9') {if (base > INT_MAX / 10 || ((base == INT_MAX / 10) && (str[i] - '0' > 7))) //这个条件会在加到一定程度时才触发{if (sign == 1) return INT_MAX;elsereturn INT_MIN;}base = 10 * base + (str[i++] - '0');}return base * sign;
}
分析:
注意这里的判断条件:
base == INT_MAX / 10 && (str[i] - '0' > 7),最后一个为什么是这个?
LeetCode8——String to Integer (atoi)(自己编写atoi函数)相关推荐
- String to Integer (atoi) leetcode java
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 8. String to Integer (atoi)
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- LeetCode算法入门- String to Integer (atoi)-day7
LeetCode算法入门- String to Integer (atoi)-day7 String to Integer (atoi): Implement atoi which converts ...
- 【细节实现题】LeetCode 8. String to Integer (atoi)
LeetCode 8. String to Integer (atoi) Solution1:我的答案 参考链接:http://www.cnblogs.com/grandyang/p/4125537. ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 1 clas ...
- 008 String to Integer (atoi) [Leetcode]
题目内容: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...
- LeetCode 8. String to Integer (atoi)(字符串)
LeetCode 8. String to Integer (atoi)(字符串) LeetCode 8 String to Integer atoi字符串 问题描述 解题思路 参考代码 By Sca ...
- 在C ++中将String转换为Integer并将Integer转换为String
In this article, we will understand how to convert an integer to a string in C++. We often come acro ...
- java中String,int,Integer,char、double类型转换
java中String,int,Integer,char.double类型转换----https://www.cnblogs.com/kangyu222/p/5866025.html 转载于:http ...
最新文章
- 5、找出100到1000内的不含9的素数,存到result.txt文件中
- GNN与Transformer融合促进药物发现 | 2022几何图机器学习展望
- Java程序内存分析:使用mat工具分析内存占用
- java 如何判定消息已在队列_Java面试—消息队列
- 可实现自动设置尺寸的图片上传类
- java简单系统_Java简单学生管理系统
- 进程调度优先级JAVA_进程调度算法之短进程优先(Java)
- hive修改默认创建表格式
- bash脚本之条件判断
- mysql 使用内置函数来进行模糊搜索(locate()等)
- MongoDB:管道操作
- 猴年猴赛雷,曙光服务器全面升级E5-v4平台
- 李清照最经典的10首诗词
- 萧邦主的技术博客导航
- U74LVC1G07G-SOT353R-TG
- js 实现模糊搜索功能
- mysql集群重启失败_Mysql集群重启失败
- AJ-Report小白配置大屏手册
- 腾讯云服务器入门使用-docker安装
- 量化交易 米筐 因子分组打分(成长因子)