> 简洁易懂讲清原理,讲不清你来打我~

输入字符串,输出整数
![在这里插入图片描述](https://img-blog.csdnimg.cn/4feb56d86fca437a98f1e7f18d4288c3.png)
![](https://img-blog.csdnimg.cn/78452fd60d9744ce9dca1924025df944.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/9e762814bac5404cb3700e76515a995b.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/a9e89cb153e34f53b1f5d90062b6a806.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/4f3909035de74f8196d18d38f204e6f8.png)

> 有限状态机

简单的思路
建立状态转换图和状态转换表,哈希表存储状态转移表的行索引和新的状态,通过行索引的原始状态和列索引的字符获得新的状态状态,遇到数字放入结果的个位

准确的定义
state是当前的状态
table是状态转换表
positive是当前计算结果的绝对值
c是当前的字符
isPositive是结果是否为正数
![在这里插入图片描述](https://img-blog.csdnimg.cn/70dfd609c0914780b77cdd0d626554b3.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3pob3V6aWhvbmc=,size_16,color_FFFFFF,t_70)

```cpp
class Solution {
public:
int myAtoi(string str) {
for (char c : str)change(c);
if(isPositive)return positive;
else return -positive;
}
void change(char c) {
state = table[state][getCol(c)];
if (state == "in_number") {
positive = positive * 10 + c - '0';
positive = isPositive? min(positive, (long long)INT_MAX) : min(positive, -(long long)INT_MIN);
}
else if (state == "signed"&&c=='-')isPositive=false;
}
int getCol(char c) {
if (isspace(c)) return 0;
if (c == '+' || c == '-') return 1;
if (isdigit(c)) return 2;
return 3;
}
private:
string state = "start";
int isPositive = 1;
long long positive = 0;
unordered_map<string, vector<string>> table = {
{"start", {"start", "signed", "in_number", "end"}},
{"signed", {"end", "end", "in_number", "end"}},
{"in_number", {"end", "end", "in_number", "end"}},
{"end", {"end", "end", "end", "end"}}
};
};
```

踩过的坑

```cpp
long long positive = 0;

positive = isPositive? min(positive, (long long)INT_MAX) : min(positive, -(long long)INT_MIN);

else if (state == "signed"&&c=='-')isPositive=false;
```

1

> 喜欢简洁易懂还能讲清楚原理博客的小伙伴就关注关注这个非常高产的博主呀,下次再会~

Leetcode8. 字符串转换整数 (atoi)的更多相关文章

  1. LeetCode8.字符串转换整数(atoi) JavaScript

    请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之 ...

  2. LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))

    8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...

  3. 17、字符串转换整数 (atoi)

    17.字符串转换整数 (atoi) 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非 ...

  4. 8. 字符串转换整数 (atoi)

    8. 字符串转换整数 (atoi) 方法一 import re import math class Solution(object): def myAtoi(self, str): "&qu ...

  5. LeetCode Golang 8. 字符串转换整数 (atoi)

    8. 字符串转换整数 (atoi) 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组 ...

  6. 字符串转换整数 (atoi) C++实现 java实现 leetcode系列(八)

    字符串转换整数 (atoi) java实现 C++实现 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当 ...

  7. 前端与算法 leetcode 8. 字符串转换整数 (atoi)

    目录 # 前端与算法 leetcode 8. 字符串转换整数 (atoi) 题目描述 概要 提示 解析 解法一:正则 解法二:api 解法二:手搓一个api 算法 传入测试用例的运行结果 执行结果 G ...

  8. LeetCode8. 字符串转整数 (atoi)

    8. 字符串转整数 (atoi) 描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连 ...

  9. [Swift]LeetCode8. 字符串转整数 (atoi) | String to Integer (atoi)

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

随机推荐

  1. 什么是视觉Visual SLAM

    什么是视觉Visual SLAM What Is Visual SLAM? What are the origins of visual SLAM? and what are some other a ...

  2. 「题解」POI2005 AKC-Special Forces Manoeuvres

    本文将同步发布于: 洛谷博客: csdn: 博客园: 简书. 题目 题目链接:洛谷 P3428.官网. 题意简述 给定 \(n\) 个圆 \((x_i,y_i,r_i)\),每个圆对应一个点集 \(S ...

  3. RobotFramework常用断言关键字

    变量或者关键字内容判断关键字 1.内容包含或者不包含:should contain . should not contain 与should contain x times *** Test Case ...

  4. 实验4、Flask基于Blueprint & Bootstrap布局的应用服务

    1. 实验内容 模块化工程内容能够更好的与项目组内成员合作,Flask Blueprint提供了重要的模块化功能,使得开发过程更加清晰便利.此外,Flask也支持Bootstrap的使用. 2. 实验 ...

  5. 【NX二次开发】Block UI 线性尺寸

    属性说明 常规         类型 描述     BlockID     String 控件ID     Enable     Logical 是否可操作     Group     Logical ...

  6. Redis的过期键删除策略

    文章首发于公众号:蘑菇睡不着,欢迎来看看 前言 Redis 中都是键值对的存储形式,键都是字符串类型的,而值有很多种类型,如 string.list.hash.set.sorted set等类型.当设 ...

  7. 1.5w字 + 24张图肝翻 TCP。

    TCP 是一种面向连接的单播协议,在 TCP 中,并不存在多播.广播的这种行为,因为 TCP 报文段中能明确发送方和接受方的 IP 地址. 在发送数据前,相互通信的双方(即发送方和接受方)需要建立一条 ...

  8. 为什么Mongodb索引用B树,而Mysql用B+树?

    引言 好久没写文章了,今天回来重操旧业. 今天讲的这个主题,是<面试官:谈谈你对mysql索引的认识>,里头提到的一个坑. 也就是说,如果面试官问的是,为什么Mysql中Innodb的索引 ...

  9. MQTT 协议是个啥?这篇文章告诉你!

    文章首发于我的公众号「程序员cxuan」,欢迎大家关注呀- 说到做到! 之前有位读者给我留言说想要了解一下什么是 MQTT 协议,顺便还把我夸了一把,有点不好意思啦. 那么读者的要求必须要满足啊,所以 ...

  10. 数据库创建好之后如何创建scott用户

    SQL> conn / as sysdba Connected. SQL> @%oracle_home%\rdbms\admin\utlsampl.sql 建立完成以后会自动退出sqlpl ...