当前位置:网站首页>剑指 Offer 67. 把字符串转换成整数
剑指 Offer 67. 把字符串转换成整数
2022-07-04 22:20:00 【LuZhouShiLi】
剑指 Offer 67. 把字符串转换成整数
题目
写一个函数 StrToInt,实现把字符串转换成整数这个功能。不能使用 atoi 或者其他类似的库函数。
首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。
当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。
该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。
注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。
在任何情况下,若函数不能进行有效的转换时,请返回 0。
思路
- 首先删除空格字符
- 符号位:新建一个变量保存符号位,‘-’ 符号位记录为1
- 非数字字符:遇到首个非数字的字符,立刻返回
- 数字字符:
- 字符转数字:将该字符减去’0’即可
- 数字拼接:设置当前位字符是c,当前位数字是x,数字结果是res,则数字拼接公式是:res = res * 10 + c - ‘0’
代码
class Solution {
public int strToInt(String str) {
char[] c = str.trim().toCharArray();// 去掉空格
if(c.length == 0) return 0;
int res = 0;
int bndry = Integer.MAX_VALUE / 10;
int i = 1,sign = 1;
if(c[0] == '-') sign = -1; // 符号位
else if(c[0] != '+') i = 0;
for(int j = i; j < c.length; j++)
{
if(c[j] < '0' || c[j] > '9')
{
break;// 不是数字位 直接跳出循环 然后直接返回
}
// 越界
if(res > bndry || res == bndry && c[j] > '7') return sign == 1 ? Integer.MAX_VALUE:Integer.MIN_VALUE;
// 累加
res = res * 10 + (c[j] - '0');
}
return sign * res;// 记录符号位
}
}
边栏推荐
- 集群的概述与定义,一看就会
- 攻防世界 MISC 高手进阶区 001 normal_png
- Flask 上下文详解
- BigFilter全局交易防重组件的介绍与应用
- 【lua】int64的支持
- 攻防世界 MISC 进阶 glance-50
- 华泰证券是国家认可的券商吗?开户安不安全?
- Close system call analysis - Performance Optimization
- Taobao commodity review API interface (item_review get Taobao commodity review API interface), tmall commodity review API interface
- Prosperity is exhausted, things are right and people are wrong: where should personal webmasters go
猜你喜欢

【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用

More than 30 institutions jointly launched the digital collection industry initiative. How will it move forward in the future?

Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf

The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal

都说软件测试很简单有手就行,但为何仍有这么多劝退的?

蓝队攻防演练中的三段作战

Embedded development: skills and tricks -- seven skills to improve the quality of embedded software code

攻防世界 MISC 进阶区 hong

Li Kou 98: verify binary search tree

Domestic database chaos
随机推荐
Wake up day, how do I step by step towards the road of software testing
Challenges faced by virtual human industry
Logo special training camp section 1 Identification logo and logo design ideas
Attack and defense world misc advanced area ditf
It is said that software testing is very simple, but why are there so many dissuasions?
Postgresqlql advanced skills pivot table
将QA引入软件开发生命周期是工程师要遵循的最佳实践
With this PDF, we finally got offers from eight major manufacturers, including Alibaba, bytek and Baidu
Concurrent optimization summary
Domestic database chaos
How diff are the contents of the same configuration item in different environments?
Mongodb aggregation operation summary
攻防世界 MISC 进阶区 Erik-Baleog-and-Olaf
攻防世界 MISC 进阶区 hong
PostgreSQL JOIN实践及原理
业务太忙,真的是没时间搞自动化理由吗?
leetcode 72. Edit distance edit distance (medium)
Force buckle 2_ 1480. Dynamic sum of one-dimensional array
Taobao commodity review API interface (item_review get Taobao commodity review API interface), tmall commodity review API interface
【OpenGL】笔记二十九、抗锯齿(MSAA)