当前位置:网站首页>Niu Ke swipes the question -- converting a string to an integer
Niu Ke swipes the question -- converting a string to an integer
2022-06-11 18:29:00 【HHYX.】
Convert a string to an integer
Topic link : Convert a string to an integer
Title Description
Convert a string to an integer , Library functions that require that integers cannot be converted using strings . Values for 0 Or if the string is not a valid value, it returns 0
Data range : The string length satisfies 0 \le n \le 100 \0≤n≤100
Advanced : Spatial complexity O(1) \O(1) , Time complexity O(n) \O(n)
Be careful :
① Any symbol... May appear in the string , Occurrence Division +/- Output directly when other symbols 0
② Possible in string +/- And can only appear at the beginning of the string .
Input description :
Enter a string , Including alphanumeric symbols , Can be null
Return value description :
If it is a legal numeric expression, it returns the number , Otherwise return to 0
Topic analysis
It is required to convert the string into an integer , Library functions, etc. cannot be used . If the string contains illegal characters, it returns 0. To implement this transformation, you can first judge the sign's positive and negative , The sign can only appear on the first character , So you can judge first . If there is no sign, the string conversion is performed directly . Use a loop to iterate through the string , To convert characters to numbers in turn, you only need to subtract characters from numeric characters 0 that will do , Then sum it up . The summation formula is sum=sum*10+(str[i]-‘0’), So we can find the sum , Finally, calculate according to the positive and negative . The code implementation is as follows :
Code implementation
int StrToInt(string str) {
int i = 0;
int sum = 0;// Return value Integer size
int flag = 1;// Positive and negative judgment basis 1 Representative is 0 It means negative
for (i = 0; i < str.size(); i++)
{
if (i != 0 && (str[i] < '0' || str[i]>'9'))// Illegal characters appear
{
return 0;
}
else
{
if (i == 0)
{
if (str[i] == '+')
{
flag = 1;
continue;
}
else if (str[i] == '-')
{
flag = 0;
continue;
}
else
{
sum = sum * 10 + (str[i] - '0');
continue;
}
}
sum = sum * 10 + (str[i] - '0');
}
}
if (flag == 0)
{
sum *= -1;
}
return sum;
}

边栏推荐
猜你喜欢
随机推荐
软件测试技术复习
Force buckle 31 next arrangement
全志科技T3开发板(4核ARM Cortex-A7)——MQTT通信协议案例
Various poses for text modification using sed
NR LDPC 打孔-punctured
DataNode的启动流程
求字符串中最大的 3 位相同数字
使用Visdom对损失函数进行监控
系统的可扩展型
On the problem that the while loop condition in keil does not hold but cannot jump out
Two methods for matlab to save imshow drawing pictures to a specified folder
SISO Decoder for min-sum(补充章节2)
SISO Decoder for a General (n, N - 1) SPC Code (Supplementary section 3)
牛客刷题——两种排序方法
Use transformers to convert TF model to pytorch model
[c language] compress strings and add markup characters
MATLAB 保存imshow绘制图片到指定文件夹中的两种方法
平衡搜索二叉树——AVL树
Getting started with CTF
使用Visdom對損失函數進行監控




![[untitled]](/img/ab/04beacfc7975cc3c54399447aa5027.png)




