当前位置:网站首页>【Day_06 0423】把字符串转换成整数
【Day_06 0423】把字符串转换成整数
2022-07-26 06:08:00 【安河桥畔】
把字符串转换成整数
题目来源
牛客网:把字符串转换成整数
题目描述
将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。 数值为 0 或者字符串不是一个合法的数值则返回 0
数据范围:字符串长度满足 0 \le n \le 100 \0≤n≤100
进阶:空间复杂度 O(1) \O(1) ,时间复杂度 O(n) \O(n)
注意:
①字符串中可能出现任意符号,出现除 +/- 以外符号时直接输出 0
②字符串中可能出现 +/- 且仅可能出现在字符串首位。
输入描述
输入一个字符串,包括数字字母符号,可以为空
输出描述
如果是合法的数值表达则返回该数字,否则返回0
示例1
输入
“+2147483647”
返回值
2147483647
示例2
输入
“1a33”
返回值
0
思路分析
- 字符串转换成数字,用ASCII值减去’0’的ASCII值即可得到
- 处理字符串首尾,如果有’+‘’-'号,进行标记
- 遍历字符串,与得到一个多位数的各个位上的数字思路类似,可以理解为其逆过程,每次对之前的result值乘以10,再加上当前数字的值作个位
代码展示
class Solution {
public:
int StrToInt(string str) {
if (str.size() == 0)
{
return 0;
}
vector<int> v;
int flag = 1;
int result = 0;
int i = 0;
//判断首位
if (!(isdigit(str[0])))
{
//第一位有符号则给i+1,从第二个位置开始遍历
i++;
if (str[0] == '-')
{
flag = -1;
}
else if (str[0] == '+')
{
flag = 1;
}
else
{
return 0;
}
}
while (i < str.size())
{
if (!(isdigit(str[i])))
{
return 0;
}
//用字符ASCII数字减去字符'0'的ASCII值,得到对应的整型数字
result = result * 10 + str[i] - '0';
i++;
}
return result * flag;
}
};
总结
- isdigit函数,判断一个字符是否为数字字符
- push_back(),vector尾插接口,vector没有头插接口,可以用insert进行替代
边栏推荐
- EM and REM
- Youwei low code: Brick life cycle component life cycle
- Docking wechat payment (II) unified order API
- Convolutional neural network (III) - target detection
- 顺序查找,折半查找,分块查找 ~
- Mysql45 talking about infrastructure: how is an SQL query executed?
- 数据库sql语言实战
- Embedded sharing collection 14
- Mysql45 talks about transaction isolation: why can't I see it after you change it?
- Leetcode:741. picking cherries
猜你喜欢

金仓数据库 KingbaseES SQL 语言参考手册 (7. 条件表达式)

Mysql45 talking about infrastructure: how is an SQL query executed?

Lemon class automatic learning after all

How to divide the disks under the devices and drives in win10 new computer

“子问题的递归处理”——判断两棵树是不是相同的树——以及 另一颗树的子树

The time complexity of two recursive entries in a recursive function
![[Oracle SQL] calculate year-on-year and month on month (column to row offset)](/img/ee/59d050e03c2a4ba04de57df1322283.png)
[Oracle SQL] calculate year-on-year and month on month (column to row offset)

基于消防GIS系统的智慧消防应用

Concurrency opening -- take you from 0 to 1 to build the cornerstone of concurrency knowledge system

对接微信支付(二)统一下单API
随机推荐
二叉树的性质 ~
“子问题的递归处理”——判断两棵树是不是相同的树——以及 另一颗树的子树
Lemon class automatic learning after all
Calling mode and execution sequence of JS
L. Link with Level Editor I dp
满二叉树 / 真二叉树 / 完全二叉树 ~
Acquisition of bidding information
Viewing the technology stack of distributed system from the crash report of station B
Taobao pinduoduo Tiktok 1688 Suning taote jd.com and other keyword search commodity API interfaces (keyword search commodity API interface, keyword search commodity list interface, classification ID s
JS的调用方式与执行顺序
招标信息获取
Mysql45 speak in simple terms index
How can programmers improve mental internal friction?
Database SQL language practice
递归处理——子问题
flex布局
WebAPI整理
Widget is everything, widget introduction
Qu Weihai, chairman and CEO of Xinyi interactive, adheres to mutual benefit and win-win results, and Qu Weihai promotes enterprise development
数据库sql语言实战