当前位置:网站首页>[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
边栏推荐
- Optical quantum milestone: 3854 variable problems solved in 6 minutes
- 分布式 | 实战:将业务从 MyCAT 平滑迁移到 dble
- Mysql45 talks about transaction isolation: why can't I see it after you change it?
- C language explanation series - comprehensive exercises, guessing numbers games
- [day_020419] sort subsequence
- K. Link with Bracket Sequence I dp
- Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4
- 递归处理——子问题
- Interview difficulties: difficulties in implementing distributed session, this is enough!
- TPS Motion(CVPR2022)视频生成论文解读
猜你喜欢

VRRP protocol and experimental configuration

Jz36 binary search tree and bidirectional linked list

Optical quantum milestone: 3854 variable problems solved in 6 minutes

Embedded sharing collection 15

光量子里程碑:6分钟内解决3854个变量问题

招标信息获取

Recursive processing - subproblem

Kingbasees SQL language reference manual of Jincang database (8. Function (10))

Matlab vector and matrix

PHP 多任务秒级定时器的实现方法
随机推荐
2022年下半年系统集成项目管理工程师(软考中级)报名条件
Viewing the technology stack of distributed system from the crash report of station B
Solutions to the failure of copy and paste shortcut keys
Widget is everything, widget introduction
【Day05_0422】C语言选择题
Latex同时合并表格的多行多列
[highly available MySQL solution] centos7 configures MySQL master-slave replication
Embedded sharing collection 15
多目标检测
Using dynamic libraries in VS
Taobao JD pinduoduo Tiktok taote 1688 and other multi platform commodity app details API interfaces (commodity details page data interface, commodity sales interface, keyword search commodity sales in
H. Take the elevator greedy
YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors
【Day_04 0421】计算糖果
Optical quantum milestone: 3854 variable problems solved in 6 minutes
【Day_03 0420】字符串中找出连续最长的数字串
【Day06_0423】C语言选择题
数据库sql语言实战
【Oracle SQL】计算同比与环比(列转行进行偏移)
[Oracle SQL] calculate year-on-year and month on month (column to row offset)