当前位置:网站首页>剑指 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 进阶 glance-50
- Redis的持久化机制
- 嵌入式开发:技巧和窍门——提高嵌入式软件代码质量的7个技巧
- [cooking record] - stir fried 1000 pieces of green pepper
- La prospérité est épuisée, les choses sont bonnes et mauvaises: Où puis - je aller pour un chef de station personnel?
- Practice and principle of PostgreSQL join
- Erik baleog and Olaf, advanced area of misc in the attack and defense world
- 我在linux里面 通过调用odspcmd 查询数据库信息 怎么静默输出 就是只输出值 不要这个
- 记录:关于Win10系统中Microsoft Edge上的网页如何滚动截屏?
- How to reset the password of MySQL root account
猜你喜欢

Logo special training camp Section IV importance of font design

It is said that software testing is very simple, but why are there so many dissuasions?

Logo special training camp section III initial creative techniques

Detailed explanation of heap sort code

Google Earth Engine(GEE)——基于 MCD64A1 的 GlobFire 日常火灾数据集

Mongodb aggregation operation summary

Naacl-22 | introduce the setting of migration learning on the prompt based text generation task

串口数据帧

Locust性能测试 —— 环境搭建及使用

Persistence mechanism of redis
随机推荐
MYSQL架构——逻辑架构
Breakpoint debugging under vs2019 c release
Practice and principle of PostgreSQL join
Challenges faced by virtual human industry
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
【OpenGL】笔记二十九、抗锯齿(MSAA)
记录:关于Win10系统中Microsoft Edge上的网页如何滚动截屏?
模拟摇杆控制舵机
共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
醒悟的日子,我是怎么一步一步走向软件测试的道路
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
SPSS安装激活教程(包含网盘链接)
Summary of index operations in mongodb
The table is backed up in ODPs. Why check m in the metabase_ Table, the logical sizes of the two tables are inconsistent, but the number of
Introducing QA into the software development lifecycle is the best practice that engineers should follow
Common open source codeless testing tools
阿里推出新品牌“瓴羊”,致力成为“数字化领头羊”
不同环境相同配置项的内容如何diff差异?
The proofreading activity of data science on the command line second edition was restarted
如何实现轻松管理1500万员工?