LeetCode | 8.String to Integer (atoi)

Redmi Book Pro 15 2022 SSD更换实战指南 NVMe固态硬盘是现代笔记本存储系统的核心组件,其性能受PCIe通道分配、散热设计与BIOS兼容性多重制约。在轻薄本中,板载SSD焊死、无扩展插槽已成为典型设计瓶颈,导致容量不足、高温降频与带宽压缩等问题频发。更换第三方NVMe SSD需兼顾主控兼容性(如群联PS5013)、闪存同源性及导热结构匹配,尤其关注BIOS SATA模式(AHCI/RST切换)、屏蔽罩应力安装与0.15mm导热垫精度等工程细节。该操作适用于追求存储性能重构与长期稳定性的进阶用户,本质是面向硬件底层的系统级优化实践。 阅读详情

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.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.

spoilers alert… click to show requirements for atoi.

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.

解题思路:注意前置空格的处理/溢出的处理以及正负号的处理.15ms AC
class Solution {
public:
        int myAtoi(string str)
        {
          int len = str.length();
          int it = 0;
          int flag = 2;//0表示正数,1表示负数
          if(len == 0)//字符串为空
          {
            return 0;
          }
          while(str[it] == ' ')
          {
            it++;
          }
          if(it == len)//全是空格
          {
            return 0;
          }
          if(str[it] == '+')
          {
            flag = 0;
            it++;
          }
          else if(str[it] == '-')
          {
            flag = 1;
            it++;
          }
          else if(!(str[it]>='0'&&str[it]<='9'))//首字母是非数字
          {
            return 0;
          }
          string tmp = "";
          while(str[it]>='0' && str[it]<='9')
          {
            tmp += str[it++];
          }
          len = tmp.length();//实际要转换的字符串
          long long res = 0;
          if(flag == 1)
          {
            if(tmp == "2147483648")
            {
              return INT_MIN;
            }
          }
          int of = 0;
          for(int i=len-1;i>=0;i--)
          {
            res += (tmp[i]-'0')*pow(10.0,len-i-1);
            if(res > INT_MAX)
            {
              of = 1; break;
            }
          }
          if(of == 1)//overflow
          {
              if(flag != 1)
                return INT_MAX;
              else
                return INT_MIN;
            //return INT_MAX*pow((-1),flag);
          }
          else
          {
            return res*pow((-1),flag);
          }
          return res;
        }
};

然后再附上网友的简短代码

int myAtoi(string str) {
    long result = 0;
    int indicator = 1;
    for(int i = 0; i<str.size();)
    {
        i = str.find_first_not_of(' ');
        if(str[i] == '-' || str[i] == '+')
            indicator = (str[i++] == '-')? -1 : 1;
        while('0'<= str[i] && str[i] <= '9') 
        {
            result = result*10 + (str[i++]-'0');
            if(result*indicator >= INT_MAX) return INT_MAX;
            if(result*indicator <= INT_MIN) return INT_MIN;                
        }
        return result*indicator;
    }
}
华为OD机试双机位C卷-虚拟文件系统(C/C++/Py/Java/Js/Go) 华为OD机试(机考)双机位C卷 【虚拟文件系统】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD机试真题【虚拟文件系统】。 阅读详情

相关推荐

算法策略 | MACD跨周期短线交易策略开发(股指+商品双版)

量化策略开发,高质量社群,交易思路分享等相关内容

松鼠宽客的博客 2540

Leetcode 8. String to Integer (atoi)

@[TOC](Leetcode 8. String to Integer (atoi)) 题目说明 Implement atoi which converts a string to an integer. 题目可以简短概括为将一个输入的字符串转化为数值型输出,其中特殊情况有: 1.首字符可以是空格/数字/正负符号(有效输入为多空格直至出现后面两种情况之一) 2.首字符一旦不是上述三种情况之一直...

babi_qq的博客 275

Notion vs 飞书文档深度对比:个人知识管理到底该选谁?2026实测报告

本文基于2026年30天深度实测,对比了Notion与飞书文档在个人知识管理(PKM)场景下的核心差异。Notion以“块”为基石,提供高度自由的结构化能力,适合构建个性化知识网络;飞书文档则深度融入协作生态,强调流程效率与团队协同。报告从设计哲学、核心功能、AI能力、成本生态等多维度剖析,助你根据自身对灵活性与协作性的需求做出选择。

weixin_29248313的博客 1400

LeetCode 8 String to Integer (atoi)

LeetCode 8

萤火虫啊飞呀飞 289

Jetson AGX Orin 新手避坑:解决‘找不到nvidia-jetpack包’的完整修复指南

本文详细介绍了Jetson AGX Orin设备在安装JetPack开发套件时遇到的'nvidia-jetpack包缺失'问题的完整解决方案。通过分析问题根源、提供分步修复指南以及系统优化建议,帮助开发者快速解决这一常见问题,充分发挥Jetson AGX Orin的边缘计算性能。

weixin_42520909的博客 463

9、横向束流光学:原理、模拟与耦合效应

本文系统介绍了横向束流光学中的关键概念,包括色度、色散、发射度、动量压缩因子以及四维耦合效应。通过MATLAB代码详细演示了相关参数的计算与模拟流程,展示了粒子在加速器中的运动特性及其对束流稳定性的影响。同时,讨论了如何通过磁铁配置优化束流光学性能,为加速器的设计与优化提供了理论基础和实践指导。

resnet7explorer的博客 120

LeetCode 力扣 8. 字符串转换整数 (atoi) string to integer

大家觉得写还可以,可以点赞、收藏、关注一下吧! 也可以到我的个人博客参观一下,估计近几年都会一直更新!和我做个朋友吧!https://motongxue.cn 文章目录????8. 字符串转换整数 (atoi)题目描述提示:样例分析提交结果 ????8. 字符串转换整数 (atoi) 题目描述 请你来实现一个 atoi 函数,使其能将字符串转换成整数。 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。接下来的转化规则如下: 如果第一个非空字符为正或者负号时,则将该符号与之后

motongxue的博客 301

【硬件】LT3763中文手册

1)精确控制输入和输出电流;2) 3000:1真彩色PWM™调光;3) ±1.5%电压调节精度;4) ±6%电流调节精度;5)6V至60 V输入电压范围;6)宽输出范围最高可达55V;7)<2µA关断电流;8)用于负载电流热控制的控制引脚;9)输入和输出电流监控和限制;10)开路、短路和C/10故障检测;11)适用于LED应用的PWM驱动器输出;12)热增强型28引脚FE封装。

qq_39200110的博客 491

【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

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

MAVLink协议详解

MAVLink是为微型飞行器MAV(Micro Air Vehicle)设计的(LGPL)开源的通讯协议。是无人飞行器和地面站(Ground Control Station ,GCS)之间,以及无人飞行器之间通讯常用的协议。APM、PIXHAWK飞控,Mission Planner、QGroundControl地面站均使用了MAVLink协议进行通讯。 MAVLink源码下载地址(现已更新至v2.0):https://github.com/mavlink/qgroundcontrol   用户手册:ht

程序猿老樊的博客 7710

【中项第三版】系统集成项目管理工程师 | 第 1 章 信息化发展

【中项第三版】系统集成项目管理工程师 | 第 1 章 信息化发展

静谧、淡雅 6581

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

LeetCode 8. String to Integer (atoi) 字符串转整形 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary unt...

goasleep的博客 216

游戏客户端开发工程师笔试题

1、已知有一个关键字序列:(27,23,34,14,1,55, 20,19,85,83,10,68)散列存储在一个哈希表中,若散列函数为H(key)=key%7,并采用链地址法来解决冲突,则在等概率情况下查找成功的平均查找长度为(4、出现死锁时,除了可以采用资源剥夺的方法解决死锁问题外,还可以使用的方法是()是被限定只能在表的一端进行插入运算,在另一端进行删除运算的线性表.5、大端模式,是指数据的高字节保存在内存的(3、在二叉查找树中, 最好情况下复杂度为。)地址中,而数据的低字节保存在内存的(

博客 1877

LeetCode 8. String to Integer (atoi)

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-white...

qq_40280096的博客 148

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

WINCC7.3破解文件

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

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

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

LeetCode #8 String to Integer (atoi)

LeetCode #8 String to Integer (atoi) Question 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 ...

weixin_30414305的博客 115

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

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

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...

Alatebloomer的博客 334

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的博客 530

Leetcode #8 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 do not see below and ask yourself what are the possible

小昕成长史 286

【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)

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

lifi的博客 436

leetcode解题报告8. String to Integer (atoi)

leetcode解题报告8. String to Integer (atoi)题目地址 难度是medium题目描述把一个字符串,变成数字 比如”123”, 返回数字123我的思路粗看,是很简单的模拟类题目。但是提了几次才过,难点在于考虑各种边界情况(特殊情况) 比如,字符串为空,前面是空字符,前面有符号标示(正负号),字符串包含其他符号,还有溢出等问题。我的代码class Solution {

sysu_xiaoming的博客 263

python leetcode 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 found. Then, starting from thi...

DreamerLHQ的博客 426

8. String to Integer (atoi) Leetcode Python

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

hyperbolechi的专栏 956

LeetCode(8)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 do not see below and ask yourself what are the possible

指尖飞舞 4704

leetcode 8 string to integer(atoi)

题目链接:https://leetcode.com/problems/string-to-integer-atoi/ 题目的意思很是简单:Implement atoi to convert a string to an integer, 实现函数 atoi。 如果对atoi函数不是很了解的话,还真是不怎么好写。不过还好有比较多的提示。 参照提示来写就是比较有思路了。 提示的话,放

598
上一篇: LeetCode | 7.Reverse integer(整数翻转)
下一篇: LeetCode | 9. Palindrome Number(回文串)
PKU_CXK
博客等级 码龄10年 87粉丝 142原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值