String to Integer (atoi)——LeetCode进阶路⑧
原题链接https://leetcode.com/problems/string-to-integer-atoi/
说实话,看到这道题之前,看这通过率有点慌,到底是因为啥 让一道medium的题目这么“厉害”咧,接着往下瞧~
- problem description:
Implement
atoiwhich 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.
实现将字符串转换为整数的atoi。
函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符。然后,从这个字符开始,取一个可选的初始加号或减号,后面跟着尽可能多的数字,并将它们解释为一个数值。
字符串可以在组成整数的字符之后包含其他字符,这些字符将被忽略,并且对该函数的行为没有影响。
如果str中的非空格字符的第一个序列不是有效的整数,或者由于str为空或只包含空格字符而不存在这样的序列,则不执行转换。
如果不能执行有效的转换,则返回零值。
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.
注意:
只有空格字符' '被认为是空格字符。
假设我们正在处理一个环境,只能在32位带符号整数存储整数范围:(231−231−1)。如果数值的范围可表示的值,INT_MAX(231−1)或INT_MIN返回(231−)。
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.- thinking:读罢题目,应该会明确,之所以准确率不高应该与情况较多有关系。
1.删掉空格
2.判断是否有正负号,但是只能有一个,否则判为无效转换返回0
3.一旦遇到非数字,停止转换
4.出现数据溢出时,返回边界值 - 源码附录:
class Solution {
public int myAtoi(String str) {
str = str.trim();//删空格 if(str == null || str.length() < 1){
return 0;
} int flag = 1;
int index = 0;
long result = 0; if(str.charAt(index) == '-'){
flag = -1;
index ++;
}
else if(str.charAt(index) == '+'){
index ++;
} for(;index<str.length();index ++){
int t = str.charAt(index) - '0';
if(t>9 || t<0){
break;
} if(flag == 1){
result = result*10 + t;
if(result > Integer.MAX_VALUE){
return Integer.MAX_VALUE;
}
}
else{
result = result*10 - t;
if(result < Integer.MIN_VALUE){
return Integer.MIN_VALUE;
}
}
} int temp = (int)result;
return temp;
}
}#四级over 寒假之前被实训和复习安排的明明白白,LeetCode……寒假见
String to Integer (atoi)——LeetCode进阶路⑧的更多相关文章
- String to Integer (atoi) leetcode
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- String to Integer (atoi) ---- LeetCode 008
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- String to Integer (atoi) leetcode java
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- 8. String to Integer (atoi) ---Leetcode
Implement atoi to convert a string to an integer. 题目分析: 题目本身很简单就是将一个字符串转化成一个整数,但是由于字符串的千差万别,导致在实现的时候 ...
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- LeetCode: String to Integer (atoi) 解题报告
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))
8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
随机推荐
- Spring Boot 3.0深度实战:从核心特性到生产级调优
一.Spring Boot 3.0核心特性解读 1.1 JDK 17 LTS支持(实测性能提升) 记录类(Record)与Spring Data JPA完美适配 模式匹配简化类型判断 密封类(Seal ...
- 面试题53 - I. 在排序数组中查找数字 I
地址:https://leetcode-cn.com/problems/zai-pai-xu-shu-zu-zhong-cha-zhao-shu-zi-lcof/ <?php /** 面试题53 ...
- Zookeeper Java客户端连接慢、超时问题Ad-Hoc检查清单
TL;DR 排查思路: 首先确认你的设备到zookeeper的连通性是OK的,可通过命令echo srvr | nc HOST 2181,检查是否可以正常打印节点信息.windows用户可以在命令行输 ...
- 写一个简单的SQL生成工具
知识点: MyBatis 语法概览 MyBatis 是一个强大的数据持久化框架,它提供了一种半自动化的 ORM 实现方式.通过 MyBatis,开发者可以通过简单的 XML 或注解来配置和映射原生信息 ...
- 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
概述 先看下面的图片,我们去旅游选择出行模式有很多种,可以骑自行车.可以坐汽车.可以坐火车.可以坐飞机. 作为一个程序猿,开发需要选择一款开发工具,当然可以进行代码开发的工具有很多,可以选择Idea进 ...
- OpnenHarmony 开源鸿蒙北向开发——2.第一个工程HelloWorld
一.新建项目 我们打开IDE后,选择新建项目 选择这一个 设置参数 设置完成后选择Finish 项目创建后会自动下载一些东西,不用担心 二.运行 我们先什么都不用管,直接运行 先设置设备,我们这里 ...
- Python字符串前缀u、r、b、f含义(转)
1.字符串前加 u 例子: u"字符串中有中文" 含义: 前缀u表示该字符串是unicode编码,Python2中用,用在含有中文字符的字符串前,防止因为编码问题,导致中文出现乱码 ...
- 探秘Transformer系列之(18)--- FlashAttention
探秘Transformer系列之(18)--- FlashAttention 目录 0x00 概述 0.1 问题 0.2 其它解决方案 0.3 Flash Attention 0x01 背景知识 1. ...
- 网络编程-关闭连接(1)-C/C++相关系统调用
背景 在linux网络编程中,经常需要编写关闭socket的代码,比如心跳检测失败需要关闭重连:网络报异常需要关闭重连.但究竟关闭操作做了什么,却不太清楚.目前项目使用Netty框架来实现的网络编程, ...
- 《机器人SLAM导航核心技术与实战》第1季:第5章_机器人主机
<机器人SLAM导航核心技术与实战>第1季:第5章_机器人主机 视频讲解 [第1季]5.第5章_机器人主机-视频讲解 [第1季]5.1.第5章_机器人主机_X86与ARM主机对比-视频讲解 ...