8. String to Integer (atoi) ---Leetcode
Implement atoi to convert a string to an integer.
题目分析:
题目本身很简单就是将一个字符串转化成一个整数,但是由于字符串的千差万别,导致在实现的时候细节非常多。需要注意的有以下一些:
1.字符串可能由一些空格开始,然后遇到一个正号或者负号,然后是正常的数字
” +123” -> 123 ;” -41” -> -41
2.如果在数字后出现了一些其他字符,直接忽视
” +123^&34” -> 123 ; ” -41 123” -> -41
3.如果最前面的字符是无效数字,或者字符串只有空格或者是空的,都直接返回0
” &*^20” -> 0
4.如果正确的数字超出了int,如果是大于INT_MAX (2147483647),就返回2147483647;如果小于INT_MIN (-2147483648) ,就返回INT_MIN (-2147483648)
“2147483648” -> 2147483647
class Solution {
public:
int myAtoi(string str) {
long long sum=0;
int flag=0;//判断是否是第一个非空格字符
int fuhao=0;//如果等于1,表明是负数
int mine=-2147483648;//最小int
int maxe=2147483647;//最大int
for(int i=0;i<str.size();i++)
{
//如果目前为止还没有出现第一个非空格字符
if(str[i]==32 && flag==0) continue;
if((str[i]==43) || (str[i]==45)) {//43:+ 45:-
if(flag==0)//这里flag==0为真时表示遇到第一个非空格字符
{
flag=1;
if(str[i]==45)fuhao=1;
continue;
}
else return 0;//非第一个非空格字符位,又出现正负号,就是无效数字
}
//如果数字后面又出先了非数字字符,就自动忽略后面的字符,返回已统计的数字
if(str[i]<48 || str[i]>57) {
if(fuhao==1) return -sum;
else return sum;
}
sum=sum*10+str[i]-48;//如果该位字符是正常的数字,就将这个数字加入统计
flag=1;//即使第一个非空格字符不是符号,也要明确已经出现了非空格字符
if(fuhao==0){
if(sum>maxe) return maxe;
}
else{
if(-sum<mine) return mine;
}
}
if(fuhao==1) return -sum;
return sum;
}
};
8. String to Integer (atoi) ---Leetcode的更多相关文章
- String to Integer (atoi) leetcode
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- String to Integer (atoi) ---- LeetCode 008
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- String to Integer (atoi) leetcode java
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 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 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- LeetCode: String to Integer (atoi) 解题报告
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- 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 ...
随机推荐
- 2019最新Web前端经典面试试题(含答案)
1,阐述清楚浮动的几种方式(常见问题)(1)父级div定义 height原理:父级div手动定义height,就解决了父级div无法自动获取到高度的问题. 优点:简单.代码少.容易掌握 缺点:只适合高 ...
- C++实现企业链表(单向链表的另外一种实现方式)
LinkList.h #include <windows.h> #include <stdio.h> // 链表小结点 typedef struct LINKNODE { LI ...
- java中级面试题
1.Java中堆和栈有什么不同? 每个线程都有自己的栈内存,用于存储本地变量,方法参数和栈调用,一个线程中存储的变量对其它线程是不可见的.而堆是所有线程共享的一片公用内存区域.对象都在堆里创建,为了提 ...
- Vue框架之初识
介绍 vue.js 是用来构建web应用接口的一个库 技术上,Vue.js 重点集中在MVVM模式的ViewModel层,它连接视图和数据绑定模型通过两种方式.实际的DOM操作和输出格式被抽象的方式到 ...
- C++——异常处理
前言 大型和十分复杂的程序往往会产生一些很难查找的甚至是无法避免的运行时错误.当发生运行时错误时,不能简单地结束程序运行,而是退回到任务的起点,指出错误,并由用户决定下一步工作.面向对象的异常处理(e ...
- 使用Dell iDRAC服务器远程控制安装操作系统简要图解
使用Dell iDRAC服务器远程控制安装操作系统简要图解 iDARC tools iDRAC又称为Integrated Dell Remote Access Controller,也就是集成戴尔 ...
- leetcode刷题-1
小A 和 小B 在玩猜数字.小B 每次从 1, 2, 3 中随机选择一个,小A 每次也从 1, 2, 3 中选择一个猜.他们一共进行三次这个游戏,请返回 小A 猜对了几次? 输入的guess数组为 小 ...
- java kafka
https://blog.csdn.net/panchang199266/article/details/82113453 安装部署 scala语言开发 + java
- webpack 配置react脚手架(二):热更新
下面继续配置 webpack dev server hot module replacement: 首先配置dev-server 安装 npm i webpack-dev-ser ...
- Springboot简单集成ActiveMQ
Springboot简单集成ActiveMQ 消息发送者的实现 pom.xml添加依赖 <dependency> <groupId>org.springframework.bo ...