LeetCode 8 字符串转换整数 (atoi)
生活随笔
收集整理的这篇文章主要介绍了
LeetCode 8 字符串转换整数 (atoi)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
https://leetcode-cn.com/problems/string-to-integer-atoi/
解决方案
class Solution {public int myAtoi(String s) {s = s.trim();long num = 0;for (int i = (s.startsWith("-") || s.startsWith("+")) ? 1 : 0;i < s.length()&& s.charAt(i) >= '0' && s.charAt(i) <= '9'&& num >= Integer.MIN_VALUE && num <= Integer.MAX_VALUE; i++) {num = num * 10 + (s.charAt(i) - '0');}num = s.startsWith("-") ? -num : num;if (num < Integer.MIN_VALUE) {return Integer.MIN_VALUE;} else if (num > Integer.MAX_VALUE) {return Integer.MAX_VALUE;} else {return (int) num;}} }总结
以上是生活随笔为你收集整理的LeetCode 8 字符串转换整数 (atoi)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: LeetCode 6 Z 字形变换
- 下一篇: LeetCode 9 回文数