Leecode #8 String to Integer (atoi)

本文详细解析了LeetCode第八题的题目要求,介绍了如何通过C++实现字符串到整数的转换,包括处理空格、正负号及数字的读取,并确保数值在32位整数范围内。

一、 问题描述
Leecode第八题,题目为:

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.

问题理解为

函数首先去掉空格,直到找到第一个非空格字符。然后,从第一个非空格字符开始,取一个可选的初始正负号,后跟尽可能多的数字,将其转换为数值。
字符串可以包含整数的字符以外的字符,这些字符将被忽略,并且对函数功能没有影响。
如果str中的第一个非空格字符序列不是有效的整数,或者由于str为空或只包含空格字符而不存在这样的序列,则不执行转换函数。
如果不能执行有效的转换,则返回零值。
注:
1、只有字符’ '被认为是空白字符。
2、假设我们处理的环境只能存储32位带符号整数范围内的整数:[- 2^31, 2^31- 1]。如果数值超出可表示值的范围,则返回INT_MAX(231 - 1)或INT_MIN(- 231)。

例 1:

输入: “42”
输出: 42

例 2:

输入: " -42"
输出: -42
注释: 第一个非空格字符为 ‘-’, 表示减号。.
根据取尽可能多的数字的原则,我们得到数值42.

例 3:

输入: “4193 with words”
输出: 4193
注释: 转换在数字“3”处停止,因为下一个字符不是数字.

例4:

输入: “words and 987”
输出: 0
注释:第一个非空格字符是“w”,它不是一个数字
数字或正负号。因此,不能执行任何有效的转换。

例 5:

输入: “-91283472332”
输出: -2147483648
注释: 数字“-91283472332”超出了32位带符号整数的范围。
返回前INT_MIN(- 231)。

二、解题思路

1、检查空格和字符
2、找出字符串中的数字然后返回。

三、实现代码

class Solution {
public:
    int myAtoi(string str) {
        long res = 0;
        int sign = 1;
        int i = 0;
        
        	while(i < str.length() && str[i] == ' ') i++;
        	if(str[i] == '-' || str[i] == '+'){
        		sign = (str[i++] == '-') ? -1 : 1;
			}
			while(i < str.length() && '0' <= str[i] && str[i] <= '9')
			{
				res = res * 10 + (str[i++] - '0');
				if(res * sign >= INT_MAX) return INT_MAX;
            	if(res * sign <= INT_MIN) return INT_MIN; 
			}
	
		return res * sign;
    }
};

基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究(Matlab代码实现)内容概要:本文围绕“基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究”展开,提出了一种结合数据驱动方法与Koopman算子理论的递归神经网络(RNN)模型线性化方法,旨在提升纳米定位系统的预测控制精度与动态响应能力。研究通过构建数据驱动的线性化模型,克服了传统非线性系统建模复杂、计算开销大的问题,并在Matlab平台上实现了完整的算法仿真与验证,展示了该方法在高精度定位控制中的有效性与实用性。; 适合人群:具备一定自动化、控制理论或机器学习背景的科研人员与工程技术人员,尤其是从事精密定位、智能控制、非线性系统建模与预测控制相关领域的研究生与研究人员。; 使用场景及目标:①应用于纳米级精密定位系统(如原子力显微镜、半导体制造设备)中的高性能预测控制;②为复杂非线性系统的数据驱动建模与线性化提供新思路;③结合深度学习与经典控制理论,推动智能控制算法的实际落地。; 阅读建议:建议读者结合Matlab代码实现部分,深入理解Koopman算子与RNN结合的建模范式,重点关注数据预处理、模型训练与控制系统集成等关键环节,并可通过替换实际系统数据进行迁移验证,以掌握该方法的核心思想与工程应用技巧。
基于粒子群算法优化Kmeans聚类的居民用电行为分析研究(Matlb代码实现)内容概要:本文围绕基于粒子群算法(PSO)优化Kmeans聚类的居民用电行为分析展开研究,提出了一种结合智能优化算法与传统聚类方法的技术路径。通过使用粒子群算法优化Kmeans聚类的初始聚类中心,有效克服了传统Kmeans算法易陷入局部最优、对初始值敏感的问题,提升了聚类的稳定性和准确性。研究利用Matlab实现了该算法,并应用于居民用电数据的行为模式识别与分类,有助于精细化电力需求管理、用户画像构建及个性化用电服务设计。文档还提及相关应用场景如负荷预测、电力系统优化等,并提供了配套代码资源。; 适合人群:具备一定Matlab编程基础,从事电力系统、智能优化算法、数据分析等相关领域的研究人员或工程技术人员,尤其适合研究生及科研人员。; 使用场景及目标:①用于居民用电行为的高效聚类分析,挖掘典型用电模式;②提升Kmeans聚类算法的性能,避免局部最优问题;③为电力公司开展需求响应、负荷预测和用户分群管理提供技术支持;④作为智能优化算法与机器学习结合应用的教学与科研案例。; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,深入理解PSO优化Kmeans的核心机制,关注参数设置对聚类效果的影响,并尝试将其应用于其他相似的数据聚类问题中,以加深理解和拓展应用能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值