700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > LeetCode 8 字符串转换整数 (atoi)

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

时间:2022-03-23 00:27:05

相关推荐

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

https://leetcode-/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;}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。