题目英文

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: [−2^31, 2^31− 1]. If the numerical value is out of the range of representable values, INT_MAX (2^31 − 1) or INT_MIN (−2^31) 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.

题目中文

请你来实现一个 atoi 函数,使其能将字符串转换成整数。

首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。

当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。

该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。

注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。

在任何情况下,若函数不能进行有效的转换时,请返回 0。

说明

假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [−2^31, 2^31− 1]。如果数值超过这个范围,请返回 INT_MAX (2^31− 1)INT_MIN (−2^31)

示例 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) 。

示例 6:

输入: "  0000000000012345678"
输出: 12345678

示例 7:

输入: "20000000000000000000"
输出: 2147483647

算法实现

public class Solution {public int MyAtoi(string str) {str = str.Trim();if (string.IsNullOrEmpty(str))return 0;if (str[0] != '-' && str[0] != '+'){if (str[0] < '0' || str[0] > '9')return 0;}int negative = 1;long result = 0;Queue<int> q = new Queue<int>();for (int i = 0; i < str.Length; i++){if (str[i] == '-' && i == 0){negative = -1;continue;}if (str[i] == '+' && i == 0){continue;}if (str[i] < '0' || str[i] > '9'){break;}q.Enqueue(str[i] - '0');}while (q.Count != 0){int i = q.Dequeue();//去掉队列前端的零if (i == 0 && result == 0)continue;// 返回超过位数的数字if (negative == 1 && q.Count > 10){return int.MaxValue;}if (negative == -1 && q.Count > 10){return int.MinValue;}result += i * (long)Math.Pow(10, q.Count);if (negative == 1 && result > int.MaxValue){return int.MaxValue;}if (negative == -1 && result * -1 < int.MinValue){return int.MinValue;}}return (int)result * negative;        }
}

实验结果

  • 状态:通过
  • 1079 / 1079 个通过测试用例
  • 执行用时: 104 ms, 在所有 C# 提交中击败了 98.32% 的用户
  • 内存消耗: 24.3 MB, 在所有 C# 提交中击败了 24.45% 的用户


相关图文

1. “数组”类算法

  • LeetCode实战:三数之和
  • LeetCode实战:求众数
  • LeetCode实战:缺失的第一个正数
  • LeetCode实战:快乐数
  • LeetCode实战:寻找两个有序数组的中位数

2. “链表”类算法

  • LeetCode实战:两数相加
  • LeetCode实战:删除链表的倒数第N个节点
  • LeetCode实战:合并两个有序链表
  • LeetCode实战:合并K个排序链表
  • LeetCode实战:两两交换链表中的节点
  • LeetCode实战:旋转链表
  • LeetCode实战:环形链表

3. “栈”类算法

  • LeetCode实战:有效的括号
  • LeetCode实战:最长有效括号
  • LeetCode实战:逆波兰表达式求值

4. “队列”类算法

  • LeetCode实战:设计循环双端队列
  • LeetCode实战:滑动窗口最大值

5. “递归”类算法

  • LeetCode实战:爬楼梯

6. “字符串”类算法

  • LeetCode实战:反转字符串
  • LeetCode实战:翻转字符串里的单词

7. “树”类算法

  • LeetCode实战:相同的树
  • LeetCode实战:对称二叉树
  • LeetCode实战:二叉树的最大深度
  • LeetCode实战:将有序数组转换为二叉搜索树

8. “哈希”类算法

  • LeetCode实战:两数之和

9. “搜索”类算法

  • LeetCode实战:搜索二维矩阵

10. “动态规划”类算法

  • LeetCode实战:最长回文子串

11. “数值分析”类算法

  • LeetCode实战:x 的平方根

LeetCode实战:字符串转换整数 (atoi)相关推荐

  1. [DFA|有限状态机] leetcode 8 字符串转换整数(atoi)

    [DFA|有限状态机] leetcode 8 字符串转换整数(atoi) 1.题目 题目链接 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符, ...

  2. 32位有符号整数_[LeetCode] 8. 字符串转换整数 (atoi)

    题目链接:https://leetcode-cn.com/problems/string-to-integer-atoi/ 题目描述: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先 ...

  3. leetcode 8. 字符串转换整数 (atoi)

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

  4. Leetcode 8. 字符串转换整数 (atoi) (每日一题 20210615)

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

  5. Leetcode 08. 字符串转换整数 (atoi)

    原题链接 1.字符  0~~~~~9 分别对应整数 48~~~~~57 2.先过滤空白 3.确定前面所带的符号 4. long long res = 0;     res = res * 10 + s ...

  6. LeetCode 8 字符串转换整数 (atoi)

    https://leetcode-cn.com/problems/string-to-integer-atoi/ 解决方案 class Solution {public int myAtoi(Stri ...

  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. ⭐算法入门⭐《模拟》中等01 —— LeetCode 8. 字符串转换整数

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

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

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

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

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

最新文章

  1. Android:屏幕旋转
  2. Linux中的基础和小工具
  3. Java 连接数据库(SQL Server)ODBC配置详情
  4. 这次是在没有外网yum仓库的情况下搭建内网yum仓库和无人值守pxe装机
  5. 阿里云centos7通过yum安装 Mysql 8.0.11
  6. 飞机大作战游戏 1----(运用H5和Js制作)
  7. django新建一个项目_如何使用Django创建项目
  8. TCP半连接队列(syns queue)和全连接队列(accept queue)
  9. 解封装(三):AVFormatContext分析
  10. 图解 Android 广播机制
  11. 关于直播,所有的技术细节都在这里了(3)《转载》
  12. 关于treeview中的checkbox的全选问题
  13. 《MySQL数据库》关联查询
  14. [连接机顶盒]-使用 adb 命令行无线连接 EC6108V9 华为悦盒
  15. Excel比较两列的值
  16. “女人~,你在玩火”一个有磁性的声音说道——常用自动化测试工具
  17. 小白刷LeeCode(算法篇)6
  18. 小米平板2 win10 MIUI互刷教程
  19. js 获取数组最后一个元素
  20. 【机器人学习】 四足机器人(单腿三自由度)正运动学与轨迹规划仿真(solidwork三维模型+matlab代码)

热门文章

  1. linux指令 2>1 到底是个啥
  2. win使用linux共享打印机,Ubuntu 12.04以及 Window 下使用共享打印机
  3. linux qml 环境,利用Qml与Golang打造Gui客户端(二)qamel环境安装
  4. php instr函数,oracle的instr函数用法
  5. php fastcgi配置_IIS7.5配置php(FastCGI)- 自动配置
  6. 序列建模:时间卷积网络取代RNN(An Empirical Evaluation of Generic Convolutional and Recurrent)论文 pdf
  7. 读书笔记:《图解HTTP》第三章 HTTP报文
  8. Oracle Grid Control 10.2.0.5 for Linux 安装和配置指南
  9. html图片缩放6,四款css 图片按比例缩放实例(兼容ie6,7,firefox)
  10. pythonrequest爬取小说,pythonrequest爬取小说_python爬取斗破苍穹小说