当前位置:网站首页>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;
}
};
边栏推荐
- Huawei interview question: Recruitment
- Exploration and practice of inress in kubernetes
- [some notes]
- MySQL workbench data modeling function
- ROS2 Foxy depthai_ros教程
- 二叉树的链式存储
- [Maui] add click events for label, image and other controls
- Technology sharing | MySQL: how about copying half a transaction from the database?
- 关于NAND FLASH解扣的认识
- 微信模拟地理位置_伪装微信地理位置
猜你喜欢

【20220605】文献翻译——虚拟现实中的可视化:一个系统的回顾
![[20220605] Literature Translation -- visualization in virtual reality: a systematic review](/img/11/6c42957186bf530e8f9d4025a40197.png)
[20220605] Literature Translation -- visualization in virtual reality: a systematic review

Onenet Internet of things platform - the console sends commands to mqtt product devices
![[20211129] configuration du serveur distant du carnet de notes jupyter](/img/7c/79c9fcb91bde75e954dc3ecf9f5afd.png)
[20211129] configuration du serveur distant du carnet de notes jupyter

JS reverse | m3u8 data decryption of a spring and autumn network

How to install php7 and perform performance test using yum

MySQL common functions
![[Yunju entrepreneurial foundation notes] Chapter VII Entrepreneurial Resource test 1](/img/be/1194125442aaa2d7cc20b6a4a6762a.jpg)
[Yunju entrepreneurial foundation notes] Chapter VII Entrepreneurial Resource test 1

2022-06-28-06-29

Ansible相关内容梳理
随机推荐
Understanding of NAND flash deblocking
Message queue monitoring refund task batch process
【MAUI】为 Label、Image 等控件添加点击事件
[JS advanced] promise explanation
Efforts at the turn of the decade
Onenet Internet of things platform - the console sends commands to mqtt product devices
GID: open vision proposes a comprehensive detection model knowledge distillation | CVPR 2021
[20211129] configuration du serveur distant du carnet de notes jupyter
[shell programming] - shell introductory learning
Onenet Internet of things platform - create mqtts products and devices
【datawhale202206】pyTorch推荐系统:召回模型 DSSM&YoutubeDNN
[datawhale202206] pytorch recommendation system: recall model DSSM & youtubednn
Typora adds watermarks to automatically uploaded pictures
Onenet Internet of things platform - mqtts product equipment connected to the platform
Question d'entrevue de Huawei: recrutement
codeforces -- 4B. Before an Exam
Mysql database knowledge collation
AI matting tool
编译调试Net6源码
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