当前位置:网站首页>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;
}
};
边栏推荐
- Ansible相关内容梳理
- Good luck brought by years of persistence
- Teach you to complete the actual battle of image classification hand in hand -- Image Recognition Based on convolutional neural network
- [datawhale202206] pytorch recommendation system: recall model DSSM & youtubednn
- Technology sharing | MySQL: how about copying half a transaction from the database?
- 华为面试题: 招聘
- Message queue monitoring refund task batch process
- AI matting tool
- 【datawhale202206】pyTorch推荐系统:精排模型 DeepFM&DIN
- One year anniversary of bitbear live studio, hero rally order! I invite you to take a group photo!
猜你喜欢

Common chart usage of Bi tools

codeforces -- 4B. Before an Exam

第十四章 信号(四)- 多进程任务示例

Virtualenv+pipenv virtual environment management
![[20220605] Literature Translation -- visualization in virtual reality: a systematic review](/img/11/6c42957186bf530e8f9d4025a40197.png)
[20220605] Literature Translation -- visualization in virtual reality: a systematic review

"Analysis of 43 cases of MATLAB neural network": Chapter 40 research on prediction of dynamic neural network time series -- implementation of NARX based on MATLAB

Double linked list related operations

队列操作---

The operation process of using sugar to make a large data visualization screen
![[Suanli network] technological innovation of Suanli Network -- key technology of operation service](/img/80/6e3648c88d309516d4bc29db9c153c.jpg)
[Suanli network] technological innovation of Suanli Network -- key technology of operation service
随机推荐
类的初始化与实例化
栈-------
Good luck brought by years of persistence
STM32 project practice (1) introduction and use of photosensitive resistor
Zero copy technology of MySQL
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 7
【datawhale202206】pyTorch推荐系统:多任务学习 ESMM&MMOE
[brain opening] west tide and going to the world series
[JS advanced] promise explanation
Pandas reads MySQL data
Typora realizes automatic uploading of picture pasting
LeetCode 454. Add four numbers II
【语音信号处理】3语音信号可视化——prosody
2022-06-28-06-29
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 8
華為面試題: 招聘
Exploration and practice of inress in kubernetes
Sleep quality today 79 points
Operations related to sequence table
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 2