说明

算法:String to Integer (atoi)
LeetCode地址: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 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.

解题思路

题意是把一个字符串转为整型,但要注意所给的要求,先去除最前面的空格,然后判断正负数,注意正数可能包含 +,
如果之后存在非数字或全为空则返回 0,而如果合法的值超过 int 表示的最大范围,则根据正负号返回 INT_MAX 或 INT_MIN。
从字符获取数字的取巧方式 int number = charArray[i] - '0';
时间复杂度为 O(N)。

代码实现

import java.util.Arrays;public class StringToIntegerAtoi {public int myAtoni(String str) {str = str.trim();char[] charArray = str.toCharArray();int i = 0;int simbol = 1;if (i < charArray.length && (charArray[i] == '+' || charArray[i] == '-')) {simbol = charArray[i] == '+' ? 1 : -1;i++;}int data = 0;int bigIntMinusTen = Integer.MAX_VALUE / 10;for(; i < charArray.length; i++) {int number = charArray[i] - '0';if (number > 9 || number < 0) {break;}if (data > bigIntMinusTen || (data == bigIntMinusTen && ((simbol == 1 && number > 7 ) || (simbol == -1 && number > 8)))) {return simbol == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;}data = data * 10 + number;}return simbol * data;}public static void main(String[] args) {StringToIntegerAtoi obj = new StringToIntegerAtoi();String input5 = "-3924x8fc";int output5 = obj.myAtoni(input5);System.out.println("input: " + input5 + " output: " + output5);}
}

运行结果

input: -3924x8fc output: -3924

代码执行效率

Runtime: 15 ms, faster than 95.14% of Java online submissions for String to Integer (atoi).
Memory Usage: 38.6 MB, less than 17.53% of Java online submissions for String to Integer (atoi).

总结

字符串遍历,注意边界以及获取数字的取巧方式。

代码下载:
https://github.com/zgpeace/awesome-java-leetcode/blob/master/code/LeetCode/src/StringToIntegerAtoi.java

算法:String to Integer (atoi)(字符串转换整数)相关推荐

  1. LeetCode 8. String to Integer (atoi)(字符串)

    LeetCode 8. String to Integer (atoi)(字符串) LeetCode 8 String to Integer atoi字符串 问题描述 解题思路 参考代码 By Sca ...

  2. 【数据结构与算法】之深入解析“字符串转换整数 (atoi)”的求解思路和算法示例

    一.题目要求 请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数). 函数 myAtoi(string s) ...

  3. 8. String to Integer (atoi) 字符串转成整数

    [抄题]: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: T ...

  4. 8. String to Integer[M]字符串转整数

    题目 Inplement atoi which converts a string to an integer. The function first discards as many whitesp ...

  5. 8. 字符串转换整数 (atoi)(leetcode力扣算法 - java / rust)

    8. 字符串转换整数 (atoi): 请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数). 函数 myAto ...

  6. 力扣——字符串转换整数 (atoi)

    字符串转换整数 (atoi) 请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数). 函数 myAtoi(st ...

  7. Leetcode 129求根节点到叶节点数字之和、104二叉树的最大深度、8字符串转换整数(atoi)、82删除排序链表中的重复元素II、204二分查找、94二叉树的中序遍历、144二叉树的前序遍历

    Top1:Leetcode 129求根节点到叶节点数字之和 官方题解:https://leetcode.cn/problems/sum-root-to-leaf-numbers/solution/qi ...

  8. c++ 32位有符号的整数_【LeetCode】字符串分类字符串转换整数 (atoi)

    " 摘要:本文主要讲述LeetCode字符串分类字符串转换整数 (atoi)解法.主要内容如下: 题目 示例 解题 " 01 - 题目 请你来实现一个 atoi 函数,使其能将字符 ...

  9. ⭐算法入门⭐《模拟》中等01 —— LeetCode 8. 字符串转换整数

    文章目录 一.题目 1.题目描述 2.基础框架 3.原题链接 二.解题报告 1.思路分析 2.时间复杂度 3.代码详解 三.本题小知识 四.加群须知 一.题目 1.题目描述   实现一个myAtoi( ...

  10. C++字符串转换整数 (atoi)

    字符串转换整数 (atoi) 一个小更新. 今天仔细的研究了一下int型越界的情况, 顺便把力扣的一道题做了. 请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 ...

最新文章

  1. AMS重要的数据结构解析(一):ActivityRecord
  2. python opengl 截图_初试PyOpenGL二 (Python+OpenGL)基本地形生成与高度检测
  3. 乱用信用卡?黑名单见
  4. mysql权限相关操作
  5. 研究员轻松劫持2.8万台打印机
  6. RobotStudio创建目标点时出现未找到有效配置的问题
  7. 直线绘制算法-数值微分法(DDA)
  8. 前端引入阿里图标库的最便捷方式
  9. excel 删除重复项_在Excel 2013列表中删除重复项
  10. win10 labelme 使用记录
  11. 【Benewake(北醒) 】中距 TF02-i 40m工业版本CAN/485介绍以及资料整理
  12. Ubuntu 下同局域网主机访问Tomcat 服务器
  13. 速写初习(二)---线条2
  14. php空间 景安,景安提供1G免费一年的PHP和ASP空间
  15. 像电影里黑客高手一样敲代码攻击入侵网站(模拟)
  16. c语言中pair的头文件,C++中使用pair是否一定要包含头文件utility
  17. buddypress主题_BuddyPress入门指南:提示和资源
  18. PTA天梯赛、RoboCom练习
  19. X射线系统、超声波系统及MRI数字成像原理解析
  20. NC维护云平台技术分享之 NC维护云管家通信框架

热门文章

  1. python获取视频缩略图_Python代码生成视频的缩略图的实例讲解
  2. HTML关联两个标签事件,javascript – 交换2个html元素并保留事件侦听器
  3. 命令调出本地链接_大牛进化路上之Linux基础命令,看看你了解多少?
  4. Oracle中使用SQL语句修改字段类型
  5. 入门笔记 Day two
  6. 盘点遥测终端RTU怎么分类?
  7. java io知识点汇总FIle类
  8. 华为三层交换机-路由-硬件防火墙的配置
  9. 信息安全系统设计基础_exp3
  10. 修改Wordpress插件---advanced-access-manager.1.7.3---