LeetCode-8:String to Integer (atoi)(字符串转整数) -- Medium

MediaPipeUnityPlugin(最新版)摇摆拳人脸识别 阅读详情

题目:

Implement atoi which converts a string to an integer.

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned.

Note:

Only the space character ’ ’ is considered as whitespace character.
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.

例子:

Example 1:

Input: "42"
Output: 42

Example 2:

Input: "   -42"
Output: -42
Explanation: The first non-whitespace character is '-', which is the minus sign.
             Then take as many numerical digits as possible, which gets 42.

Example 3:

Input: "4193 with words"
Output: 4193
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.

Example 4:

Input: "words and 987"
Output: 0
Explanation: The first non-whitespace character is 'w', which is not a numerical 
             digit or a +/- sign. Therefore no valid conversion could be performed.

Example 5:

Input: "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
             Thefore INT_MIN (−231) is returned.

问题解析:

实现 atoi,将字符串转为整数。

思路标签

边界检查

解答:

直接转换
  • 问题简单,最主要的是需要注意边界条件;
  • 首先解决前置空格;
  • 其次判断正负号;
  • 再次接下来一个非空字符不为数字,则整体不符合转换要求;
  • 接下来遇到非数字字符,对字符串进行截断;
  • 通过long型存储和正负号flag来进行判断是否越界。
class Solution {
public:
    int myAtoi(string str) {
        if(str.empty())
            return 0;
        long digit = 0;
        int minusflag = 1;
        int i = 0;
        while(str[i] == ' ')
            i++;
        if(str[i] == '-'){
            minusflag = -1;
            i++;
        }
        else if(str[i] == '+')
            i++;

        if(str[i] - '0' < 0 || str[i] - '0' > 9)
            return 0;

        while(str[i] - '0' >= 0 && str[i] - '0' <= 9 && digit < INT_MAX && i < str.size()){
            int num = str[i] - '0';
            digit = digit*10 + num;
            i++;
        }
        if(digit*minusflag > INT_MAX)
            return INT_MAX;
        if(digit*minusflag < INT_MIN)
            return INT_MIN;
        return digit*minusflag;
    }
};
金融科技风险管理实践之监管评级全解 内部审计是公司治理的四大基石之一。因此,建议银行以问题与结果为导向,推动科技风险分级分类管理,重点防范重大中断、重大事件,并进一步加强科技风险评估与监测,加强对重要管理流程、重要系统、网络等重点管控环节的评估监测,对于评估发现的问题,及时完善管理手段,举一反三解决问题,提高风险管理的有效性。投入更多审计资源,加大对重点机构、薄弱环节、重要领域的审计力度,如:加强对境外机构的审计,加强网络安全、数据安全、业务连续性等高风险领域审计,加强数字化转型过程中的风险审计,以高水平安全,为数字金融高质量发展保驾护航。 阅读详情

相关推荐

一份针对零基础学习AI Agent详细学习计划

以下是针对零基础学习者制定的AI Agent详细学习计划,结合最新行业动态和实用资源推荐,帮助你从入门到进阶系统掌握核心技能

sexy19910923的博客 1904

【LeetCode】 8. String to Integer (atoi) 字符串转换整数 (atoi)(Medium)(JAVA)

【LeetCode】 8. String to Integer (atoi) 字符串转换整数 (atoi)(Medium)(JAVA) 题目地址: https://leetcode.com/problems/string-to-integer-atoi/ 题目描述: Implement atoi which converts a string to an integer. The functio...

qq_16927853的博客 189

单细胞测序中pseudo bulk差异分析的实战指南与审稿人应对策略

本文提供了单细胞测序中pseudo bulk差异分析的完整实战指南,旨在应对审稿人对样本量统计效力的质疑。文章详细解析了从数据聚合、使用DESeq2进行差异分析,到与单细胞水平结果进行交叉验证的全流程,并给出了应对常见问题与撰写回复的策略,帮助研究者提升结论的稳健性与说服力。

weixin_29214199的博客 825

LeetCode.8-字符串转整数(String to Integer (atoi))

这是悦乐书的第349次更新,第374篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第4题(顺位题号是8)。实现将字符串转换为整数的atoi方法。 该函数首先去掉所需丢弃的空白字符,直到找到第一个非空白字符。然后,从该字符开始,采用可选的初始加号或减号,后跟尽可能多的数字,并将它们转换为整数。 字符串可以包含在形成整数之后的其他字符,这些字符将被忽略并且对此函数的...

小川的博客 236

DRAM内存模拟持久化设备的方法

由于持久化内存介质NVM技术的发展,越来越多的科研工作者投身到NVM相关的研究当中。但由于目前工业界并没有推出广泛使用的NVM介质设备,所以科研工作都需要自己以现有的条件,模拟NVM设备使用场景。一个简单的方法是:通过修改DRAM模拟器,达到模拟NVM的目的(可以参考:DRAMSim2模拟器分析与应用)。更高端的方式是:在真实系统中,将内存划分出一块儿,用来模拟NVM设备(类似于传统的ramdis

酷炫人笨笨熊 1899

国产麒麟系统下用Rider开发C#桌面应用:从环境配置到Avalonia UI实战

本文详细介绍了在国产麒麟系统上使用JetBrains Rider开发C#桌面应用的完整流程,重点讲解了Avalonia UI框架的实战应用。从环境配置、Rider安装到Avalonia项目创建和UI开发技巧,全面覆盖了在非Windows环境下进行C#开发的关键步骤和优化建议,助力开发者高效构建跨平台桌面应用。

weixin_27791839的博客 387

【leetcode】【medium】【每日一题】8. String to Integer (atoi)​​​​​​​

8. String to Integer (atoi) Implementatoiwhichconverts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is f...

lemonade13的博客 126

基因家族的鉴定-基于Windows系统上的HMMER

基因家族的鉴定-基于Windows系统上的HMMER。

liangjinghui123的博客 3250

LeetCode Top Interview Questions 8. String to Integer (atoi) (Java版; Medium)

welcome to my blog LeetCode Top Interview Questions 8. String to Integer (atoi) (Java版; Medium) 题目描述 Implement atoi which converts a string to an integer. The function first discards as many whitespa...

littlehaes的博客 168

2026国内Claude Code保姆级教程:安装、避坑、防串台全优化

安装 Claude Code CLI + 切换 npm 国内镜像优先配置固定关闭:自动联网、自动项目扫描VSCode 内置终端直接用,不装多余插件会话隔离+指令约束,彻底解决 Rust、小程序 跨语言串台。

lpfasd123的博客 2558

【RTD】AD7793两线式铂电阻PT100/PT1000应用

两线式RTD是低成本的检测方案,适用于检测精度不高、导线长度较短的场合。

只要思想不滑坡,想法总比问题多。 4258

LeetCode算法题之8. String to Integer (atoi)(medium)

声明: 因本人为AI路上的新手,文章用于辅助个人的整理记忆,理解难免有偏差之处,都是个人拙见,如给其他同僚造成困扰,还请见谅,非常非常非常欢迎私信共同讨论,共同进步 题目描述: Implement atoi which converts a string to an integer. The function first discards as many whitespace characters...

DaMoWangZQ的博客 243

SPMI总线协议(一)

系统电源管理接口( System Power Management Interface简称SPMI)是一种双线串行接口,用于连接片上系统(SoC)处理器系统的集成电源控制器(PC)与一个或多个电源管理集成电路(PMIC)电压调节系统。SPMI 使系统能够使用单个 SPMI 总线动态调整 SoC 内部电压域的电源和衬底偏置电压。移动设备和其他便携式设计正日益采用系SPMI)协议,实现多主多从系统,根据实时系统需求监测和动态控制供电电压。设计人员可以在系统级实现更低的功率。

qflook的博客 888

LeetCode Algorithm 0008 - String to Integer (atoi) (Medium)

LeetCode Algorithm 0008 - String to Integer (atoi) (Medium) Problem Link: https://leetcode.com/problems/string-to-integer-atoi/description/ 文章目录LeetCode Algorithm 0008 - String to Integer (atoi) (Med...

DarkRabbit的专栏 308

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

LeetCode_Medium_8. String to Integer (atoi)

2019.1.24 题目描述: Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then,...

weixin_41637618的博客 208

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

[LeetCode] (medium) 8. String to Integer (atoi)

https://leetcode.com/problems/string-to-integer-atoi/ Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first no...

Frontier_Setter的博客 166

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

LeetCode (力扣) 8. String to Integer (atoi) ( C ) - Medium

同步发于 JuzerTech 网站,里面有我软、硬件学习的纪录与科技产品开箱,欢迎进去观看。 题目为给定一个字符串,把它转换成整数型态,复现 C语言中的 atoi() 函数。 这题的测资有点复杂,要考虑的情况有点多。 题目与范例如下 Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function). The .

JuzerTech的博客 212

LeetCode Medium String to Integer(atoi)

Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this...

Lee.rw的博客 211

[Leetcode学习-java]String to Integer (atoi)(字符串转整形 ascii to integer)

问题: 难度:medium 说明: 输入一个字符串,然后将字符串转为int,而且需要去掉字符串开始空格,以及字符串还可能输入其他字符,一些跟在数字后面的字符就去掉,字符串还可能非法,如果非法字符串就返回0即可。 输入案例: Example 1: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: The first non-whitespace character is '-', which i

qq_28033719的博客 190

Leetcode 8. String to Integer (atoi) [medium][java]

Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this...

inkhk的博客 123

Leet Code OJ 8. String to Integer (atoi) [Difficulty: Medium] -python

题目:https://leetcode.com/problems/string-to-integer-atoi/ 8.String to Integer (atoi) Medium 16849704Add to ListShare Implementatoiwhichconverts a string to an integer. The function first discards as many whitespace characters as necessary until the...

渣渣琪的博客 310

LeetCode 8. String to Integer (atoi)

LeetCode 8. String to Integer (atoi) c++

JY纪云的博客 589

LeetCode OJ 8 String to Integer (atoi) [Medium]

题目描述: Implement atoi toconvert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge,please do not see below and ask yourself what are the possible input

大吱的博客 375

String to Integer(atoi)

String to Integer(atoi) description: Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace charac...

codedz的博客 252

第十八周(String to Integer (atoi))

第十八周(String to Integer (atoi))目录: 本周完成题目 主要过程思路 相关代码 一、本周完成题目 本周共完成1道题目,1道Medium。 具体完成题目及难度如下表: # Title Difficulty 8 String to Integer (atoi) Medium 题目内容1、String to Integer (atoi)  Impl

lyc的博客 237

leetcode 8. String to Integer (atoi)

题目: 8. String to Integer (atoi) Medium 8685565FavoriteShare Implement atoi whichconverts a string to an integer. The function first discards as many whitespace characters as necessary until the ...

scqlovezy的博客 130
上一篇: LeetCode-7:Reverse Integer(翻转整数)
下一篇: LeetCode-12:Integer to Roman(整数转罗马数字)
大树先生的博客
博客等级 码龄9年 2692粉丝 200原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值