当前位置:网站首页>【牛客】把字符串转换成整数
【牛客】把字符串转换成整数
2022-06-24 07:07:00 【Uaena_An】
把字符串转换成整数

🧸读题
字符串转整形,遇到不是 +-的其他字符直接返回0;
而这里的+- 是正负数的意思,正数要省略+ ,负数要显示-。
🧸代码
我的思路是,管他正负,直接转,遇到其他字符返回0
当是结果要求是正数,就直接输出。
如果是负数,就用的得到的正数减去2倍的自己
class Solution {
public:
bool isNumber(char ch)
{
if(ch >= '0' && ch<= '9')
{
return true;
}
else if( ch == '+' || ch =='-')
{
return true;
}
return false;
}
int StrToInt(string str) {
//'1' - '0' = 1
int end = str.size()-1;
int ret = 1;//计算位数
int count = 0;//计数
while(0 <= end)
{
while(0 <= end && !isNumber(str[end]))
{
return 0;
}
if(str[end] != '+'&& str[end]!='-')
{
count += (str[end] - '0')*ret;
ret*=10;
}
--end;
}
if(str[0] == '-')
{
count -= 2*count;
}
return count;
}
};
🧸解读代码
class Solution {
public:
是一个判断 数字的函数 是数字/+/-返回true,否则返回false
bool isNumber(char ch)
{
if(ch >= '0' && ch<= '9')
{
return true;
}
else if( ch == '+' || ch =='-')
{
return true;
}
return false;
}
int StrToInt(string str) {
//'1' - '0' = 1
从尾部开始计算
int end = str.size()-1;
int ret = 1;//计算位数
int count = 0;//计数
end从后向前遍历,end小于0时 则停止,等于0 说明还有一个字符,也要进去
while(0 <= end)
{
判断是不是其他字符,是其他字符直接返回0
while(0 <= end && !isNumber(str[end]))
{
return 0;
}
走到这就不是其他字符 要么是数字,要么是+-
判断不是+- 就进去
if(str[end] != '+'&& str[end]!='-')
{
来到这里,ret初始值一定是1
因为是0的话 就一直输出0
count += (str[end] - '0')*ret;
各位是最后一位,所以最后一位*1还是各位
第二次要*10 所以这里ret*=10
ret*=10;
}
算玩一位,end要向前走
--end;
}
这里判断负数,如果第一位是 - 则减去2倍的自己
if(str[0] == '-')
{
count -= 2*count;
}
return count;
}
};
其他大佬的代码我没怎么看,测试了一下,非常大的数都是一个效果,就是算不出来。这个算法是 O(N)。
加油 祝你拿到心仪的offer!
边栏推荐
- Determination of monocular and binocular 3D coordinates
- DataX User Guide
- Earthly 容器镜像构建工具 —— 筑梦之路
- 微博撰写-流程图-序列图-甘特图-mermaid流程图-效果不错
- Video Fusion communication has become an inevitable trend of emergency command communication. How to realize it based on easyrtc?
- Base64编码详解及其变种(解决加号在URL变空格问题)
- leetcode 1268. Search suggestions system
- There was an error checking the latest version of pip
- Camera projection matrix calculation
- orb slam build bug: undefined reference to symbol ‘_ ZN5boost6system15system_ categoryEv‘
猜你喜欢

数据库迁移从PostgreSQL迁移到 MYSQL

教程篇(5.0) 08. Fortinet安全架构集成与FortiXDR * FortiEDR * Fortinet 网络安全专家 NSE 5

Base64编码详解及其变种(解决加号在URL变空格问题)

It is enough to read this article about ETL. Three minutes will let you understand what ETL is

K8s deployment of highly available PostgreSQL Cluster -- the road to building a dream

Pymysql inserts data into MySQL and reports an error for no reason

Mysql数据(Liunx环境)定时备份

opencv最大值滤波(不局限于图像)

Liunx Mysql安装
![Jenkins is deployed automatically and cannot connect to the dependent service [solved]](/img/fe/f294955a9bdf7492aab360e44e052d.png)
Jenkins is deployed automatically and cannot connect to the dependent service [solved]
随机推荐
Get screen width and height tool class
利用ngrok做内网穿透
Scheduled database backup script
Earthly 容器镜像构建工具 —— 筑梦之路
Base64编码详解及其变种(解决加号在URL变空格问题)
Prompt code when MySQL inserts Chinese data due to character set problems: 1366
2022.06.23(LC_144,94,145_二叉树的前序、中序、后序遍历)
mysql写的代码数据 增删查改等等
216. 组合总和 III-枚举法
不能改变虚拟机电源状态报错解决方案
基于QingCloud的地理信息企业研发云解决方案
110. 平衡二叉树-递归法
数据中台:中台实践与总结
小程序云数据,数据请求一个集合数据的方法
mysql组合索引的有序性
leetcode 1268. Search suggestions system
1528. 重新排列字符串
【NOI模拟赛】摆(线性代数,杜教筛)
一文讲透,商业智能BI未来发展趋势如何
Increase insert speed