|
| 1 | +# [面试题67. 把字符串转换成整数](https://leetcode-cn.com/problems/ba-zi-fu-chuan-zhuan-huan-cheng-zheng-shu-lcof/) |
| 2 | + |
| 3 | +## 题目描述 |
| 4 | +<!-- 这里写题目描述 --> |
| 5 | +写一个函数 StrToInt,实现把字符串转换成整数这个功能。不能使用 atoi 或者其他类似的库函数。 |
| 6 | + |
| 7 | +首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。 |
| 8 | + |
| 9 | +当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。 |
| 10 | + |
| 11 | +该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。 |
| 12 | + |
| 13 | +注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。 |
| 14 | + |
| 15 | +在任何情况下,若函数不能进行有效的转换时,请返回 0。 |
| 16 | + |
| 17 | +说明: |
| 18 | + |
| 19 | +假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 `[−2^31, 2^31 − 1]`。如果数值超过这个范围,请返回 `INT_MAX (2^31 − 1)` 或 `INT_MIN (−2^31)` 。 |
| 20 | + |
| 21 | +**示例 1:** |
| 22 | + |
| 23 | +``` |
| 24 | +输入: "42" |
| 25 | +输出: 42 |
| 26 | +``` |
| 27 | + |
| 28 | +**示例 2:** |
| 29 | + |
| 30 | +``` |
| 31 | +输入: " -42" |
| 32 | +输出: -42 |
| 33 | +解释: 第一个非空白字符为 '-', 它是一个负号。 |
| 34 | + 我们尽可能将负号与后面所有连续出现的数字组合起来,最后得到 -42 。 |
| 35 | +``` |
| 36 | + |
| 37 | +**示例 3:** |
| 38 | + |
| 39 | +``` |
| 40 | +输入: "4193 with words" |
| 41 | +输出: 4193 |
| 42 | +解释: 转换截止于数字 '3' ,因为它的下一个字符不为数字。 |
| 43 | +``` |
| 44 | + |
| 45 | +**示例 4:** |
| 46 | + |
| 47 | +``` |
| 48 | +输入: "words and 987" |
| 49 | +输出: 0 |
| 50 | +解释: 第一个非空字符是 'w', 但它不是数字或正、负号。 |
| 51 | + 因此无法执行有效的转换。 |
| 52 | +``` |
| 53 | + |
| 54 | +**示例 5:** |
| 55 | + |
| 56 | +``` |
| 57 | +输入: "-91283472332" |
| 58 | +输出: -2147483648 |
| 59 | +解释: 数字 "-91283472332" 超过 32 位有符号整数范围。 |
| 60 | + 因此返回 INT_MIN (−2^31) 。 |
| 61 | +``` |
| 62 | + |
| 63 | +## 解法 |
| 64 | +<!-- 这里可写通用的实现逻辑 --> |
| 65 | +遍历字符串,注意做溢出处理。 |
| 66 | + |
| 67 | + |
| 68 | +### Python3 |
| 69 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 70 | + |
| 71 | +```python |
| 72 | +class Solution: |
| 73 | + def strToInt(self, str: str) -> int: |
| 74 | + if str is None or len(str.strip()) == 0: |
| 75 | + return 0 |
| 76 | + str = str.strip() |
| 77 | + i = res = 0 |
| 78 | + negative = str[0] == '-' |
| 79 | + i += 1 if str[0] == '-' or str[0] == '+' else 0 |
| 80 | + while i < len(str) and str[i].isdigit(): |
| 81 | + r = int(str[i]) |
| 82 | + if res > (2**31 // 10) or (res == (2**31 // 10) and r > 7): |
| 83 | + return 2**31 - 1 if not negative else -2**31 |
| 84 | + res = res * 10 + r |
| 85 | + i += 1 |
| 86 | + return -res if negative else res |
| 87 | +``` |
| 88 | + |
| 89 | +### Java |
| 90 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 91 | + |
| 92 | +```java |
| 93 | +class Solution { |
| 94 | + public int strToInt(String str) { |
| 95 | + if (str == null || "".equals(str.trim())) { |
| 96 | + return 0; |
| 97 | + } |
| 98 | + str = str.trim(); |
| 99 | + int res = 0, i = 0, flag = 1; |
| 100 | + int n = str.length(); |
| 101 | + if (str.charAt(i) == '-') { |
| 102 | + flag = -1; |
| 103 | + } |
| 104 | + if (str.charAt(i) == '-' || str.charAt(i) == '+') { |
| 105 | + ++i; |
| 106 | + } |
| 107 | + while (i < n && Character.isDigit(str.charAt(i))) { |
| 108 | + int r = str.charAt(i) - '0'; |
| 109 | + // 溢出处理 |
| 110 | + if (res > Integer.MAX_VALUE / 10 || (res == Integer.MAX_VALUE / 10) && r > 7) { |
| 111 | + return flag > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE; |
| 112 | + } |
| 113 | + res = res * 10 + r; |
| 114 | + ++i; |
| 115 | + } |
| 116 | + |
| 117 | + return flag > 0 ? res : -res; |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +### ... |
| 123 | +``` |
| 124 | +
|
| 125 | +``` |
0 commit comments