当前位置:网站首页>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;
}
};
边栏推荐
- 2022-06-28-06-29
- 比特熊直播间一周年,英雄集结令!邀你来合影!
- 第十四章 信号(四)- 多进程任务示例
- 微信小程序 – 80个实用的微信小程序项目实例
- Onenet Internet of things platform - mqtts product equipment connected to the platform
- 華為面試題: 招聘
- 类的初始化与实例化
- [Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 8
- Accept different views with an open mind
- [Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 6
猜你喜欢

Consolidate -c operator

【datawhale202206】pyTorch推荐系统:召回模型 DSSM&YoutubeDNN
![[106] 360 check font - check whether the copyright of local Fonts is commercially available](/img/a7/615e8000647b56f03a6a1d3dd81b6d.jpg)
[106] 360 check font - check whether the copyright of local Fonts is commercially available

2022-06-28-06-29

Common chart usage of Bi tools

【语音信号处理】3语音信号可视化——prosody

The operation process of using sugar to make a large data visualization screen

codeforces -- 4B. Before an Exam

MySQL workbench data modeling function

redis探索之缓存一致性
随机推荐
Wechat simulated geographical location_ Camouflage wechat location
Chapter 14 signals (IV) - examples of multi process tasks
leetcode 406. Queue reconstruction by height
ASTM D 3801固体塑料垂直燃烧试验
Typora adds watermarks to automatically uploaded pictures
codeforces -- 4B. Before an Exam
硬阈值(Hard Thresholding)函数解读[通俗易懂]
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 8
Pandas reads MySQL data
Chain storage of binary tree
微信小程序 – 80个实用的微信小程序项目实例
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 2
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 6
[datawhale202206] pytorch recommendation system: precision model deepfm & DIN
顺序表有关操作
List of QT players [easy to understand]
[speech signal processing] 3 speech signal visualization -- prosody
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 5
Eurake partition understanding
Wechat applet - 80 practical examples of wechat applet projects