Leetcode : 8 String to Integer (atoi) C++

C++版 - Leetcode 8: String to Integer (myAtoi,C库函数atoi模拟) (剑指offer 面试题49) 解题报告 leetcode 8: String to Integer (myAtoi) (剑指offer 面试题49) 解题报告 8. String to Integer (atoi) 提交网址: https://leetcode.com/problems/string-to-integer-atoi/ Total Accepted: 100027 Total Submissions: 741432 Diffi 阅读详情

Description

Implement atoi to convert 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 cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

Requirements for atoi:

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. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

分析

就是手动实现一遍string to int的函数,考虑各种情况:
从第一个非空字母开始考虑,跳过之前的空格
全为空格或者为空串 –> 0
第一个非空字母开始的序列不能构成合法的数字 –>0
考虑数字前的正负号
超过INT_MAX 或INT_MIN就各自返回这两个值
合法数字之后的其他字母不产生影响

可以提交后根据Leetcode上的wrong answer样例来逐步调整

代码

#include<limits>
#include<cctype>
class Solution {
public:
    int myAtoi(string str) {
      if (str == "")return 0;
    bool neg = false;//记录是否有负号
    int len = str.length();
    long long temp = 0;//防止运算过程越界
    int i = 0,n=0;
    for ( i = 0; i < len; i++)
        if (!isblank(str[i]))break;//i记录第一个非空位置
    for (n = 0; n < len; n++)
        if (isdigit(str[n]))break;//n记录第一个数字位置

    if (i >= len - 1 && (!isdigit(str[len - 1]))) return 0;//全是空格
    if ((n - i) > 1 || (n - i == 1 && str[n - 1] != '-' && str[n - 1] != '+'))return 0;//数字前面负号不合法
    if (n > 0 && str[n - 1] == '-')neg = true;
    for (n; n < len; n++)
    {
        if(isdigit( str[n]))temp = temp * 10 + (str[n] - '0');//计算
        else break;
        if (neg == true && -temp <=INT_MIN)return INT_MIN;//注意等号
        if (neg!=true&&temp >= INT_MAX)return INT_MAX;

    }
    int ans = (neg == true) ? -(int)temp : (int)temp;//添加上负号
    return ans;
    }
};
VSCode玩转ESP-ADF音频开发:从环境搭建到成功播放MP3的避坑全流程 本文详细介绍了如何在VSCode环境下使用ESP-ADF框架进行ESP32音频开发,从环境搭建到成功播放MP3的全流程。重点解析了`IDF_PATH`和`ADF_PATH`的配置问题,以及如何解决`xTaskCreateRestrictedPinnedToCore`等常见编译错误,帮助开发者快速上手ESP-ADF音频开发。 阅读详情

相关推荐

保姆级教程:在Windows/Linux双系统下搞定Microsemi Libero SoC v11.8安装与白银版License激活

本文提供了一份详细的Microsemi Libero SoC v11.8在Windows/Linux双系统下的安装与白银版License激活指南。涵盖环境准备、安装流程、License跨平台配置及常见问题排查,帮助开发者高效搭建FPGA开发环境。

weixin_30603633的博客 420

LeetCode 8 String to Integer (atoi) (C,C++,Java,Python)

Problem: Implement atoi to convert 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 pos

算法之道 3186

欧姆龙 CJ1W-SCU模块使用通配符+结束码实现非固定长度数据的接收

实验设备:CJ2M-CPU33、CJ1W-SCU21、XW2Z-200T 实验目的:了解 CJ1W-SCU 通配符使用 1.硬件接线: CJ1W-SCU21单元号拨码置于8,使用串口1(232口)。 2.软件设置: ① 新建工程,选择PLC型号CJ2M-CPU33,切换到编程模式,如下图: 、 ② 创建I/O并进行相关配置,如下图: ③ 设置为用户设置,协议宏模式,8位数据位,2位停止位,偶校验9600波特率,如下图: 、 ④ 打开CX-Protocal软件,新建工程: .

weixin_42118352的博客 1986

Leetcode-8-String to Integer(atoi) C++

Implement atoi to convert 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 ca

悟 1431

手把手教你用ArduPilot搭建低成本无人车(附APM飞控+有刷电调配置清单)

本文提供了一份基于ArduPilot开源平台搭建低成本智能无人车的详细实战指南。核心内容包括APM架构飞控的选型、双向有刷电调的兼容性配置与校准、系统连接逻辑,以及通过Mission Planner地面站进行参数调校和自主导航功能设置的完整流程,帮助爱好者快速实现从手动控制到自主行驶的跨越。

java5的博客 851

新手必看!Unity中Tag和Layer的7个实际应用场景(含MOBA案例)

本文深入探讨Unity中Tag和Layer的7个实际应用场景,特别以MOBA游戏开发为例,详细解析如何利用LayerMask进行高效场景管理和碰撞检测优化。从双相机方案到射线检测的精准控制,帮助开发者掌握这些基础功能的进阶用法,提升游戏性能和开发效率。

weixin_28723171的博客 313

Leetcode 8. String to Integer (atoi)( C++版)

Implement atoi to convert 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 inpu

海阔天空sky的博客 550

F280049C Buffered Digital-to-Analog Converter (DAC)

使用DAC模块很简单,只需要配置参考电压,并写入DACVALS寄存器一个数字值即可。参考电压可配置为外部参考VDAC和内部参考电压1.65V和2.5V,并结合增益作为DAC模块最终的参考电压DACREF。举个例子,配置参考电压为内部参考电压1.65V,增益设置为2,则参考电压DACREF=3.3V,若向DACVALS写入数字值2048,则输出的DAC模块的输出带电压DACOUT=DACVALS×DACREF4096=1.65VDACOUT=

xht2403267701的博客 2370

Leetcode:8. 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...

旺仔馒头 308

地震勘探中的频谱分析:从基础到应用

本文深入探讨了地震勘探中的频谱分析技术,从基础原理到实际应用。通过傅里叶变换等工具,频谱分析能有效识别不同地震波的频率特征,指导数据采集和处理。文章结合油田勘探案例,展示了如何利用振幅谱和相位谱提升信噪比,并介绍了智能滤波等前沿技术在地震数据处理中的应用。

weixin_42557830的博客 409

R语言数据可视化:绘制世界地图的两种方案

本文介绍了两种使用R语言绘制世界地图的方案:包含边界边框和不包含边界边框。无论您选择哪种方案,使用ggplot2包和相关的地理数据包,我们可以轻松地绘制出漂亮的世界地图。R语言提供了丰富的包和函数,使我们能够轻松地绘制世界地图,并根据需求添加边界边框。在本文中,我将介绍两种绘制世界地图的方案:一种包含边界边框,一种不包含边界边框。在上述代码中,我们使用了与方案一相同的数据准备和地图绘制步骤,但省略了添加边界边框的代码。函数的调用,我们可以绘制不包含边界边框的世界地图。方案二:绘制不包含边界边框的世界地图。

CodeMaven的博客 2226

[C++] String to Integer (atoi)

leetcode 原题链接:https://leetcode.com/problems/string-to-integer-atoi/ Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a chall

成为一个合格的IT工作者 1564

Linux ARM平台开发系列讲解(GPIO子系统) 2.3.2 GPIO子系统常用函数解析

1. 概述 该章节会记录一年常用的GPIO子系统常用函数,会更具情况不断更新 static inline int gpio_get_value(unsigned int gpio) 功能 :用于获取某个GPIO引脚上的值 gpio : gpio引脚号 返回值:非负值,得到的 GPIO 值;负值,获取失败。 static inline void gpio_set_value(unsigned int gpio, int value) static inline int gpio_cansleep(unsi

DSMGUOGUO的博客 670

[C++]LeetCode 8:String to Integer (atoi)(字符串转int)

Problem: Implement atoi to convert 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

emmaalways的专栏 563

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

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

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

ganlanA的博客 216

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

【力扣Leetcode题解系列之0008—String to Integer (atoi):字符串转换整数 (atoi) 解题攻略】

本文介绍了力扣第8题“字符串转换整数(atoi)”的解题方法。题目要求实现一个类似C/C++中atoi的函数,将字符串转换为32位有符号整数。解题需处理以下关键点:去除前导空格、判断正负号、提取连续数字字符并进行边界检查。文章提供了Python正则表达式法和常规遍历法两种解法,并给出了Python、Java、C、C++四种语言的代码实现。正则表达式法简洁高效,而常规遍历法则更直观地展现了处理流程。两种方法都需要注意32位整数的溢出问题,当结果超出[-2^31, 2^31-1]范围时返回对应的边界值。

youngerwang的博客 895

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

【LeetCode】8. String to Integer (atoi)(C++)

地址: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 firs...

Ethan95的博客 595

LeetCode 8 — String to Integer (atoi)(C++ Java Python)

题目:http://oj.leetcode.com/problems/string-to-integer-atoi/ Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please

龙之梦 4865

LeetCode--String to Integer (atoi)

String to Integer (atoi) 1 题目 Implementatoiwhichconverts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character i...

Vilin的博客 304

String to Integer (atoi) -- LeetCode

原题链接:http://oj.leetcode.com/problems/string-to-integer-atoi/  这道题还是对于Integer的处理,在Reverse Integer这道题中我有提到,这种题的考察重点并不在于问题本身,而是要注意corner case的处理,整数一般有两点,一个是正负符号问题,另一个是整数越界问题。思路比较简单,就是先去掉多余的空格字符,然后读符号(

Code Ganker 1万+

leetcode 8. String to Integer (atoi)

leetcode 8. String to Integer (atoi)标签(空格分隔): leetcodeleetcode 8 String to Integer atoi 原题意思 分析 值得注意的溢出问题 代码 1. 原题意思:实现atoi() 实现将字符串转化为int型整数,至于多少种情况自己考虑。。2.分析: 空的字符串: “” ,判定为0 普通的正负数: “-123”, “123”, “

lifi的博客 436

LeetCode题解 String-to-Integer(atoi)

原文在此, 可以来我Blog翻翻哦。 hhh, 开始一天一道LeetCode吧, 恩, 忘记了之前算到第几天了, 那么从头开始吧, 今天是第一天. 今天的题目是(8. String to Integer (atoi))[https://leetcode.com/problems...] 题目描述: Implement atoi which...

weixin_33922672的博客 76

【Leetcode】8. String to Integer (atoi)

题目地址: https://leetcode.com/problems/string-to-integer-atoi/ 给定一个字符串sss,实现atoi函数。如果得数大于了231−12^{31}−1231−1则返回231−12^{31} − 1231−1,如果得数小于了−231-2^{31}−231则返回−231-2^{31}−231。 参考https://blog.csdn.net/qq_46105170/article/details/111714680。代码如下: public class Solu

数学、算法爱好者的博客 210

【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

LeetCode 7.Reverse Integer and 8.String to Integer(atoi)

LeetCode 7.Reverse Integer LeetCode 8.String to Integer(atoi)

My Blog 444

LeetCode题目: 8. String to Integer (atoi)

LeetCode题目: 8. String to Integer (atoi) 原题链接:https://leetcode.com/problems/string-to-integer-atoi/description/ 解题思路:(以下来自百度)原型: int atoi(const char *nptr); 函数说明: 参数nptr字符串,如果第一个非空格字符不存在或者不是数字也不是正负号则

MyCodecOdecoDecodE的博客 529
上一篇: LeetCode : 7 Reverse Integer C++
下一篇: LeetCode :9 回文数 C++
BeforeEasy
博客等级 码龄10年 92粉丝 207原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值