当前位置:网站首页>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;
}

边栏推荐
- 【无标题】
- Introduction to social engineering practice
- Reading summary of nacos2.x source code
- 互联网_业务分析概述
- 为何TI的GPMC并口,更常被用于连接FPGA、ADC?我给出3个理由
- New work of "the father of LSTM": a new method towards self correcting neural network
- ACL 2022:评估单词多义性不再困扰?一种新的基准“DIBIMT”
- 学习使用LSTM和IMDB评论数据进行情感分析训练
- v-for中的key的作用和原理
- Quanzhi Technology T3 Development Board (4 Core ARM Cortex - A7) - mqtt Communication Protocol case
猜你喜欢

Async leads to unexpected function results and changes the intention of the original code; await is only valid in async functions and the top level bodies of modules

EasyCwmp源码分析
![[golang] leetcode - 349 Intersection of two arrays (hash table)](/img/92/03de54c9f08eae5bc920b04d2b8493.png)
[golang] leetcode - 349 Intersection of two arrays (hash table)

Force deduction 23 questions, merging K ascending linked lists
![[c language] output the average score and the student data below or equal to the average score with the structure](/img/c4/263301a22b61c86a3e0df6ad2596f1.png)
[c language] output the average score and the student data below or equal to the average score with the structure

SISO Decoder for SPC (补充章节1)

牛客刷题——把字符串转换成整数

SISO Decoder for min-sum(补充章节2)

TI AM64x——最新16nm处理平台,专为工业网关、工业机器人而生

Quanzhi Technology T3 Development Board (4 Core ARM Cortex - A7) - mqtt Communication Protocol case
随机推荐
Some problems of DC-DC bootstrap capacitor
使用Visdom对损失函数进行监控
[C语言]用结构体把平均分和低于等于平均分的学生数据输出
[c language] compress strings and add markup characters
[C语言]用结构体把最高分的学生输出,可有多个最高分
使用Transformers将TF模型转化成PyTorch模型
Force deduction questions -- create a string based on a binary tree
网络和并发编程常见面试题
[C语言]压缩字符串并添加标记字符
Function and principle of key in V-for
DC-DC自举电容(BOOT)几个问题
H. 264 concept
[C语言]用结构体按分数高低降序输出学生的姓名和分数
Monitoring loss functions using visdom
Sa-Token 单点登录 SSO模式二 URL重定向传播会话示例
Several commands related to domain name
[c language] output the students with the highest scores with a structure. There can be multiple highest scores
Easycwmp source code analysis
Radio button text background changes at the same time
Reading summary of nacos2.x source code