当前位置:网站首页>[day_060423] convert string to integer
[day_060423] convert string to integer
2022-07-26 06:12:00 【On the Bank of Anhe Bridge】
Convert a string to an integer
Title source
Cattle from : 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
Output description
If it is a legal numeric expression, it returns the number , Otherwise return to 0
Example 1
Input
“+2147483647”
Return value
2147483647
Example 2
Input
“1a33”
Return value
0
Thought analysis
- String to number , use ASCII Value minus ’0’ Of ASCII You can get
- Handle the beginning and end of the string , If there is ’+‘’-' Number , marked
- Traversal string , It is similar to the idea of getting numbers on each bit of a multi digit number , It can be understood as its inverse process , Every time before result Value times 10, Add the value of the current number as a bit
Code display
class Solution {
public:
int StrToInt(string str) {
if (str.size() == 0)
{
return 0;
}
vector<int> v;
int flag = 1;
int result = 0;
int i = 0;
// Judge first
if (!(isdigit(str[0])))
{
// If the first sign is given i+1, Start traversing from the second position
i++;
if (str[0] == '-')
{
flag = -1;
}
else if (str[0] == '+')
{
flag = 1;
}
else
{
return 0;
}
}
while (i < str.size())
{
if (!(isdigit(str[i])))
{
return 0;
}
// Use characters ASCII Subtract characters from numbers '0' Of ASCII value , Get the corresponding integer number
result = result * 10 + str[i] - '0';
i++;
}
return result * flag;
}
};
summary
- isdigit function , Determine whether a character is a numeric character
- push_back(),vector Tail connector ,vector No head connector , It can be used insert To replace
边栏推荐
- 移动web
- 【Day_06 0423】把字符串转换成整数
- Jincang database kingbasees SQL language reference manual (5. Operators)
- Byte interview question - judge whether a tree is a balanced binary tree
- [the most complete and detailed] ten thousand words explanation: activiti workflow engine
- JS的调用方式与执行顺序
- [highly available MySQL solution] centos7 configures MySQL master-slave replication
- Leetcode:940. How many subsequences have different literal values
- Xiao He shows his sharp corners and says hello to flutter app
- [2023 Jerry technology approval test questions in advance] ~ questions and reference answers
猜你喜欢
![[Oracle SQL] calculate year-on-year and month on month (column to row offset)](/img/ee/59d050e03c2a4ba04de57df1322283.png)
[Oracle SQL] calculate year-on-year and month on month (column to row offset)

Mysql45 talking about global lock, table lock and row lock

Amd zen4 game God u reached 208mb cache within this year, which is unprecedented

时序动作定位 | 用于弱监督时态动作定位的细粒度时态对比学习(CVPR 2022)

VRRP protocol and experimental configuration

VS中使用动态库

YOLOv6:又快又准的目标检测框架开源啦

CV (1)- Introduction

递归函数中 有两个递归入口的时间复杂度

机械制造企业如何借助ERP系统,做好生产管理?
随机推荐
Intelligent fire protection application based on fire GIS system
Mobile web
Leetcode 347. top k high frequency elements
【Day06_0423】C语言选择题
redis 哨兵集群搭建
[day03_0420] C language multiple choice questions
L. Link with Level Editor I dp
递归函数中 有两个递归入口的时间复杂度
[day02_0419] C language multiple choice questions
L. Link with Level Editor I dp
【2023杰理科技提前批笔试题】~ 题目及参考答案
WebAPI整理
【Oracle SQL】计算同比与环比(列转行进行偏移)
Embedded sharing collection 14
YOLOv6:又快又准的目标检测框架开源啦
CCTV dialogue ZTE: why must the database be in your own hands?
机械制造企业如何借助ERP系统,做好生产管理?
Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4
基于消防GIS系统的智慧消防应用
How can machinery manufacturing enterprises do well in production management with the help of ERP system?