当前位置:网站首页>[Niuke] convert string to integer
[Niuke] convert string to integer
2022-06-24 08:59:00 【Uaena_ An】
Convert a string to an integer

🧸 Reading questions
String reshaping , Encounter is not +- Other characters of the return directly 0;
And here it is +- Is a positive or negative number , Positive numbers should be omitted + , Negative numbers are displayed -.
🧸 Code
My idea is , Whatever , Go straight to , Return... When encountering other characters 0
When the result is required to be Positive numbers , Output directly .
If it is negative , Just subtract from the positive number 2 times Oneself
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;// Calculate the number of digits
int count = 0;// Count
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;
}
};
🧸 Decoding code
class Solution {
public:
It's a judgment A function of numbers It's the number. /+/- return true, Otherwise return to 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
Starting from the tail
int end = str.size()-1;
int ret = 1;// Calculate the number of digits
int count = 0;// Count
end Traverse from back to front ,end Less than 0 when Then stop , be equal to 0 The description has another character , Go in, too
while(0 <= end)
{
Determine if there are other characters , Other characters are returned directly 0
while(0 <= end && !isNumber(str[end]))
{
return 0;
}
There are no other characters here Or numbers , Or +-
Judgment is not +- Just go in
if(str[end] != '+'&& str[end]!='-')
{
Come here ,ret The initial value must be 1
the reason being that 0 Words All the time 0
count += (str[end] - '0')*ret;
You are the last , So the last one *1 Or you guys
The second time *10 So here ret*=10
ret*=10;
}
It's a joke ,end Go forward
--end;
}
Here is a negative number , If the first one is - Then subtract 2 Times your own
if(str[0] == '-')
{
count -= 2*count;
}
return count;
}
};
I don't think much about the code of other big guys , Tested it , Very large numbers are an effect , I just can't figure it out . The algorithm is O(N).
come on. I wish you get what you want offer!
边栏推荐
- Liunx Mysql安装
- 1704. 判断字符串的两半是否相似
- Essay - Reflection
- 普通人没有学历,自学编程可以月入过万吗?
- Sword finger offer 55 - I. depth DFS method of binary tree
- [use picgo+ Tencent cloud object to store cos as a map bed]
- Using skills of xargs -- the way to build a dream
- 1528. 重新排列字符串
- How does the tunnel mobile inspection track robot monitor 24 hours to ensure the safety of tunnel construction?
- 玄铁E906移植----番外0:玄铁C906仿真环境搭建
猜你喜欢

【量化投资】离散傅里叶变换求数组周期

MySQL | view notes on Master Kong MySQL from introduction to advanced

Spark - the number of leftouterjoin results is inconsistent with that of the left table

4274. suffix expression

What is SRE? A detailed explanation of SRE operation and maintenance system

MBA-day25 最值问题-应用题

KaFormer个人笔记整理

JS to find and update the specified value in the object through the key

"Unusual proxy initial value setting is not supported", causes and Solutions

Telnet port login method with user name for liunx server
随机推荐
2020中国全国各省市,三级联动数据,数据机构(数据来自国家统计局官网)
Database migration from PostgreSQL to MySQL
【LeetCode】541. 反转字符串 II
小程序wx.show
【MySQL从入门到精通】【高级篇】(一)字符集的修改与底层原理
4275. Dijkstra序列
How does the tunnel mobile inspection track robot monitor 24 hours to ensure the safety of tunnel construction?
Liunx Mysql安装
【LeetCode】387. 字符串中的第一个唯一字符
2022.06.23(LC_144,94,145_二叉树的前序、中序、后序遍历)
Installation of sophus package in slam14 lecture
MySQL | 存储《康师傅MySQL从入门到高级》笔记
OpenCV每日函数 结构分析和形状描述符(7) 寻找多边形(轮廓)/旋转矩形交集
The list of open source summer winners has been publicized, and the field of basic software has become a hot application this year
PM2 deploy nuxt3 JS project
Jenkins is deployed automatically and cannot connect to the dependent service [solved]
第七章 操作位和位串(三)
IDEA另起一行快捷键
听说你还在花钱从网上买 PPT 模板?
“不平凡的代理初始值设定不受支持”,出现的原因及解决方法