LeetCode刷题-008字符串转为整数

实现 atoi,将字符串转为整数。
在找到第一个非空字符之前,需要移除掉字符串中的空格字符。如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即为整数的值。如果第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。
字符串可以在形成整数的字符后面包括多余的字符,这些字符可以被忽略,它们对于函数没有影响。
当字符串中的第一个非空字符序列不是个有效的整数;或字符串为空;或字符串仅包含空白字符时,则不进行转换。
若函数不能执行有效的转换,返回 0。
说明:
假设我们的环境只能存储 32 位有符号整数,其数值范围是 [?231, 231 ? 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 class Solution {
 2 public:
 3     int myAtoi(string str)
 4     {
 5         #define INT_MAX 2147483647
 6         #define INT_MIN -2147483648
 7         if(!str.empty())
 8         {
 9             str.erase(0,str.find_first_not_of(" "));
10             str.erase(str.find_last_not_of(" ") + 1);
11         }
12         int flag=1;
13         long long result=0;
14         if(int len=str.size())
15         {
16             if(str[0]>=‘0‘ && int(str[0])<=‘9‘)  result=result*10+str[0]-‘0‘;
17             else if(str[0]==‘+‘) flag=1;
18             else if(str[0]==‘-‘) flag=-1;
19             else return 0;
20             for(int i=1;i<len;i++)
21             {
22                 if(str[i]>=‘0‘ && str[i]<=‘9‘)
23                 {
24                     result=result*10+str[i]-‘0‘;
25                     if(result>INT_MAX) return (flag==-1) ? INT_MIN : INT_MAX ;
26                 }
27                 else break;
28             }
29
30         }
31         return result*flag;
32     }
33 };

原文地址:https://www.cnblogs.com/nkqlhqc/p/9085352.html

时间: 2024-08-24 04:29:10

LeetCode刷题-008字符串转为整数的相关文章

LeetCode刷题总结-字符串篇

本文梳理对LeetCode上有关字符串习题的知识点,并给出对应的刷题建议.本文建议刷题的总数为32题.具体知识点如下图: 1.回文问题 题号:5. 最长回文子串,难度中等 题号:214. 最短回文串,难度困难 题号:564. 寻找最近的回文数,难度困难 2.子串问题(类似子集) 题号:76. 最小覆盖子串,难度困难 题号:115. 不同的子序列,难度困难 题号:522. 最长特殊序列 II,难度中等 题号:1163. 按字典序排在最后的子串,难度困难 3.表达式求值问题 题号:12. 整数转罗马

LeetCode刷题之字符串

第一题:反转字符串 示例: 输入:["h","e","l","l","o"] 输出:["o","l","l","e","h"] 来源:点击这里 答案:简单 void reverseString(char* s, int sSize){ int j=sSize-1; for (int i=0;i<sSiz

leetcode刷题之 字符串反转

请编写一个函数,其功能是将输入的字符串反转过来. 示例: 输入:s = "hello" 返回:"olleh" 注:这里之所以不使用倒叙遍历strs,是因为字符串拼接的效率很低,str+=strs[i]的效率及其低下. class Solution { public String reverseString(String s) { char[] strs=s.toCharArray(); for(int i=0;i<strs.length;i++){ if(i&

[LeetCode] 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 input cases. Notes: It is intended for this problem to be spe

Leetcode篇:字符串转为整数

@author: ZZQ @software: PyCharm @file: myAtoi.py @time: 2018/9/20 20:54 要求:实现 atoi,将字符串转为整数. 1)根据需要丢弃任意多的空格字符,直到找到第一个非空格字符为止 2)如果第一个非空字符是正号或负号,选取该符号: 3) 将其与后面尽可能多的连续的数字组合起来,这部分字符即为整数的值. 4) 如果第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数. 5) 字符串可以在形成整数的字符后面包括多余

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode刷题笔记】Multiply Strings

Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 题解:就是让实现一个大整数乘法. 假设两个数num1和num2的长度分别是len1和len2,那么最后得到的答案,在最高位有进位的时候,就是len1+len2位,否则是len1+len2

【leetcode刷题笔记】N-Queens

The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of