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

输入字符串,输出整数
![在这里插入图片描述](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. 实验4、Flask基于Blueprint & Bootstrap布局的应用服务

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

  2. 【NX二次开发】获取用户输入的字符串uc1603

    效果: 源码: extern DllExport void ufsta(char *param, int *returnCode, int rlen) { UF_initialize(); strin ...

  3. 【VBA】最大行,最大列

    最大行: Range("B" & Cells.Rows.Count).End(xlUp).Row 最大列 colu = Range("XFD2").En ...

  4. 注解式项目开发!详细解析Java中各个注解的作用和使用方式

    @Target 作用: 指明了修饰的这个注解的使用范围, 即被描述的注解可以用在哪里 @Target(ElementType.Type) ElementType取值的类型: TYPE: 类,接口或者枚 ...

  5. NAT介绍与配置

    一,NAT定义 二.NAT的分类 三,NAT配置实验 一,NAT定义 NAT(Network Address Translation),网络地址转换技术,随着Internet的发展,IPv4地址枯竭已 ...

  6. .net core mysql entity映射时字符串被截断

    参考地址:https://stackoverflow.com/questions/40833262/net-core-entity-framework-mysql-string-fields-stor ...

  7. C++中运算符的重载

    运算符重载相当于运算符的函数重载,用于对已有的运算符进行重新定义,赋予其另一种功能,以适应不同的数据类型.我们之前提到过C++中的函数重载,可以根据形参的不同调用不同的函数,那么运算符重载跟函数重载的 ...

  8. 跟我一起学Go系列:Go gRPC 安全认证机制-SSL/TLS认证

    Go gRPC 系列: 跟我一起学Go系列:gRPC 拦截器使用 跟我一起学Go系列:gRPC 入门必备 第一篇入门说过 gRPC 底层是基于 HTTP/2 协议的,HTTP 本身不带任何加密传输功能 ...

  9. MyBatis框架的使用解析!数据库相关API的基本介绍

    动态SQL if 根据条件包含where子句的一部分 <select id="findActiveBlogLike" resultType="Blog"& ...

  10. SpringBoot:Service层使用@Autowired 注解 mapper对象爆红问题

    问题点 这个报错可能导致程序运行大面积爆红 这个报错会逼疯强迫症 解决方法 为避免程序运行报错 ,需要在Application.class添加注解@MapperScan(mapper包位置) @Spr ...