当前位置:网站首页>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;
}
};
边栏推荐
- 【MAUI】为 Label、Image 等控件添加点击事件
- MySQL的零拷贝技术
- 手机便签应用
- VS Code 设置单击打开新文件窗口,不覆盖前一个窗口
- The operation process of using sugar to make a large data visualization screen
- ASP. Net core 6 from entry to enterprise level practical development application technology summary
- Onenet Internet of things platform - mqtt product equipment upload data points
- 编译调试Net6源码
- (混更一篇)多个txt文本转一个表格
- Understanding of NAND flash deblocking
猜你喜欢
MySQL common functions
《MATLAB 神经网络43个案例分析》:第40章 动态神经网络时间序列预测研究——基于MATLAB的NARX实现
redis探索之缓存一致性
Four years after graduation: work, resign, get married, buy a house
IOS interview
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 7
2022-06-28-06-29
Onenet Internet of things platform - mqtt product equipment upload data points
Joint Time-Frequency and Time Domain Learning for Speech Enhancement
codeforces -- 4B. Before an Exam
随机推荐
Huawei interview question: Recruitment
[Yu Yue education] financial management reference materials of Ningbo University of Finance and Economics
華為面試題: 招聘
ASTM D 3801固体塑料垂直燃烧试验
循环链表--
双链表有关操作
栈-------
Application of stack -- bracket matching problem
微信模拟地理位置_伪装微信地理位置
队列的链式存储
Double linked list related operations
CPI教程-异步接口创建及使用
顺序表有关操作
I wish you all a happy reunion
List of QT players [easy to understand]
Ansible的playbook
[20211129] configuration du serveur distant du carnet de notes jupyter
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 6
[speech signal processing] 3 speech signal visualization -- prosody
Typora realizes automatic uploading of picture pasting