当前位置:网站首页>[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!
边栏推荐
- Solution: Nan occurs in loss during model training
- 【牛客】把字符串转换成整数
- Prompt code when MySQL inserts Chinese data due to character set problems: 1366
- 数据中台:中台实践与总结
- 数据中台:数据治理概述
- A tip to read on Medium for free
- 【E325: ATTENTION】vim编辑时报错
- [pytorch basic tutorial 30] code analysis of DSSM twin tower model
- Qingcloud based "real estate integration" cloud solution
- 4274. suffix expression
猜你喜欢

【LeetCode】415. 字符串相加

普通人没有学历,自学编程可以月入过万吗?

【E325: ATTENTION】vim编辑时报错

Opencv maximum filtering (not limited to images)

【Pytorch基础教程31】YoutubeDNN模型解析
![[pytoch basic tutorial 31] youtubednn model analysis](/img/18/dbeab69894583f6e5230772ce44652.png)
[pytoch basic tutorial 31] youtubednn model analysis

2022-06-23: given a nonnegative array, select any number to make the maximum cumulative sum a multiple of 7, and return the maximum cumulative sum. N is larger, to the 5th power of 10. From meituan. 3

input的聚焦后的边框问题

MySQL | store notes of Master Kong MySQL from introduction to advanced

On the routing tree of gin
随机推荐
YOLOX backbone——CSPDarknet的实现
数据中台:数据中台技术架构详解
1844. replace all numbers with characters
Mysql数据(Liunx环境)定时备份
数据中台:数据中台全栈技术架构解析,附带行业解决方案
Deep learning and neural networks: the six most noteworthy trends
Sword finger offer 55 - I. depth DFS method of binary tree
"I can't understand Sudoku, so I choose to play Sudoku."
opencv最大值滤波(不局限于图像)
【NOI模拟赛】寄(树形DP)
Summary of methods in numpy
4275. Dijkstra序列
Data middle office: the data middle office practice scheme of Minsheng Bank
What is graph neural network? Figure what is the use of neural networks?
Opencv daily function structure analysis and shape descriptor (7) finding polygon (contour) / rotating rectangle intersection
数据中台:民生银行的数据中台实践方案
uniapp 开发多端项目如何配置环境变量以及区分环境打包
SLAM14讲中Sophus包的安装问题
解决:模型训练时loss出现nan
MySQL | view notes on Master Kong MySQL from introduction to advanced