当前位置:网站首页>Nc100 converts strings to integers (ATOI)
Nc100 converts strings to integers (ATOI)
2022-07-01 12:34:00 【I'm not Xiao Haiwa~~~~】

describe
Write a function StrToInt, Realize the function of converting string to integer . Out of commission atoi Or other similar library functions . The incoming string may consist of the following parts :
1. Several spaces
2.( Optional ) A symbolic character (’+’ or ‘-’)
3. Numbers , Letter , Symbol , A string expression consisting of spaces
4. Several spaces 
Example 1
Input :
"82"
Return value :
82
Example 2
Input :
" -12 "
Return value :
-12
explain :
Remove the space before and after , by -12
Example 3
Input :
"4396 clearlove"
Return value :
4396
explain :
6 The following character does not belong to a valid integer part , Remove , But return the valid part extracted earlier
Example 4
Input :
"clearlove 4396"
Return value :
0
Example 5
Input :
"-987654321111"
Return value :
-2147483648
Code:
class Solution {
public:
/** * The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly * * * @param s string character string * @return int integer */
int StrToInt(string s) {
// write code here
int64_t num=0;
int64_t min=-2147483648;
int res;
string s_="";
for(int i=0;i<s.length();i++)
{
if(s[i]!=' ')
{
s_=s.substr(i);
break;
}
}
bool flag=true;
if(s_.length()>2)
{
if(!(s_[1]>='0'&&s_[1]<='9')&&!(s_[0]>='0'&&s_[0]<='9'))
{
return 0;
}
}
for(int i=0,j=0;i<s_.length();i++)
{
if(s_[i]=='-'&&i==0)
{
flag=false ;
continue;
}
if(s_[i]=='+'&&i==0)
{
continue;
}
if(s_[i]>='0'&&s_[i]<='9')
{
num=num*10;
num+=s_[i]-'0';
int64_t temp=num;
if(!flag)
{
temp=0-num;
}
if(temp<min)
{
res=-2147483648;
cout<<res<<endl;
return res;
}
if(temp>(pow(2,31)-1))
{
res=pow(2,31)-1;
cout<<res<<"====="<<endl;
return res;
}
}
else
{
break;
}
}
if(!flag)
{
num=0-num;
}
res=num;
return res;
}
};
边栏推荐
- C serialization simple experiment
- MySQL workbench data modeling function
- [Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 3
- Teach you to complete the actual battle of image classification hand in hand -- Image Recognition Based on convolutional neural network
- Onenet Internet of things platform - mqtt product devices send messages to message queues MQ
- Eurake partition understanding
- AI matting tool
- Self organization is the two-way rush of managers and members
- 《MATLAB 神经网络43个案例分析》:第40章 动态神经网络时间序列预测研究——基于MATLAB的NARX实现
- Share several tools for designing exquisite circuit diagrams
猜你喜欢

MySQL workbench data modeling function

C serialization simple experiment

Share several tools for designing exquisite circuit diagrams

【脑洞大开】《西潮》及《走向世界丛书》

Queue operation---

【datawhale202206】pyTorch推荐系统:召回模型 DSSM&YoutubeDNN

Leetcode force buckle (Sword finger offer 31-35) 31 Stack push pop-up sequence 32i II. 3. Print binary tree from top to bottom 33 Post order traversal sequence 34 of binary search tree The path with a

Self organization is the two-way rush of managers and members

双链表有关操作

Use of easyexcel
随机推荐
6.30 simulation summary
Chapter 14 signals (IV) - examples of multi process tasks
Virtualenv+pipenv virtual environment management
LeetCode 454. Add four numbers II
手机便签应用
[20220605] Literature Translation -- visualization in virtual reality: a systematic review
AI抠图工具
2022-06-28-06-29
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 3
Message queue monitoring refund task batch process
Wechat simulated geographical location_ Camouflage wechat location
JPA and criteria API - select only specific columns - JPA & criteria API - select only specific columns
華為面試題: 招聘
Fatal error: execution: there is no such file or directory
Onenet Internet of things platform - the console sends commands to mqtt product devices
Pandas reads MySQL data
Onenet Internet of things platform - mqtts product equipment connected to the platform
硬阈值(Hard Thresholding)函数解读[通俗易懂]
Sleep quality today 79 points
MySQL common functions