当前位置:网站首页>牛客,把字符串转换成整数
牛客,把字符串转换成整数
2022-07-13 19:21: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
解题思路:
我们先抛开错误,即返回值为0的事件,我们只看正确的事件,有以下两种情况。
(一)符号只出现在下标为0的位置”+123””-123”;
(二)符号还出现在其他位置”+123+456””-123-456”
对于这两种情况,我们都是可以先忽符号,最后根据下标为0处的符号是+还是-来进行最终运算。
列如”-123-456”,我们就可以先忽略负号,把他简化成”123456”,最后因为他第一个位置是”-”,然后再加上,就成了”-123456”;
所以可以看出,我们需要知道一共有多少个数字,即位数
class Solution {
public:
int StrToInt(string str) {
int sum=0;//设置一个临时数,计算总值,最后提供返回值
size_t pos=str.size();//确定有几位数
for(size_t i=0;i<str.size();i++)
{
if(str[i]=='-'||str[i]=='+')//如果+或者-占位,就需要--pos
{
pos--;
}
}
};这样我们就能算出pos的位数,然后再来计算去掉+ -号后的数是多少,即sum
if(str[i]>='0'&&str[i]<='9')
{
str[i]-='0';//将字符转化为数字,不明白的可以查ask码
sum+=str[i]*pow(10,pos-1);
pos--;
}最后根据首位置的符号,来进行返回值
if(str[0]=='-')
{
return -sum;
}
else
{
return sum;
}总代码:
class Solution {
public:
int StrToInt(string str) {
int sum=0;//设置一个临时数,最后提供返回值
size_t pos=str.size();//确定有几位数
for(size_t i=0;i<str.size();i++)
{
if(str[i]=='-'||str[i]=='+')
{
pos--;
}
else if(str[i]>='0'&&str[i]<='9')
{
str[i]-='0';
sum+=str[i]*pow(10,pos-1);
pos--;
}
else//除去正确的,剩下的都是非法字符串,就统一返回0
{
return 0;
}
}
if(str[0]=='-')
{
return -sum;
}
else
{
return sum;
}
}
};边栏推荐
- Interprocess communication (very practical)
- Response. Write specific introduction
- np.unravel_index() 求出数组某元素(或某组元素)拉成一维后的索引值在原本维度(或指定新维度)中对应的索一般与np.argmax(A) 或 np.argmin(A) 配合使用
- Orphan process, zombie process and process exit (for interview)
- Hcip fourth day experiment
- PG operation and maintenance -- service start and stop
- spark期末考试选择题精选
- Hj3 explicit random number hj03
- How to set various displays in 3dmax2021?
- Flink SQL 求上一个月 是哪个函数?
猜你喜欢

HCIP第六天笔记

Séquence de traversée de l'ordre initial et de l'ordre moyen pour déterminer un arbre binaire (restauration de l'arbre binaire)

Judge whether two binary trees are isomorphic, and three implementation methods (recursion, queue, stack)

HCIP第三天笔记

第三讲:最短距离

Interprocess communication (very practical)

【canvas】canvas和svg基本使用

Hcip second day notes

ADB环境配置

dtcloud的消息机制(三)
随机推荐
一文带你读懂深度学习中的张量(tensor)是什么,它的运算是怎样的,如何理解张量,张量的维度,浅显易懂
Flutter RenderFlex overflowed by pixels on the bottom键盘弹出警告异常
odoo eval详解
音视频学习(六)——PCM音频基础
Password key hard coding check
np.unravel_index() 求出数组某元素(或某组元素)拉成一维后的索引值在原本维度(或指定新维度)中对应的索一般与np.argmax(A) 或 np.argmin(A) 配合使用
3dmax2021 中的各种显示相关如何设置?
How to set various displays in 3dmax2021?
babylon.js高度图
Web security - DOS regular expression denial of service attack
林子雨spark scala版编程小结
Is it safe to open an account for stock speculation through the online account manager?
Hcip day 4 notes
The computer regularly clears wechat data
Judge whether two binary trees are isomorphic, and three implementation methods (recursion, queue, stack)
程序的运行过程
电脑定时清理微信数据
What happens when you unplug the power? Gaussdb (for redis) dual life keeps you prepared
第三讲:最短距离
Shutter renderflex overflowed by pixels on the bottom keyboard pop-up warning exception