/*** @Author :田宇寒.* @Date :Created in 10:46 2021年6月3日* @Description: 字符串转整数* @Source:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/string-to-integer-atoi 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。* @Modified By:* @Version: 1.0*/public class code8 {public static void main(String[] args) {Solution solution = new Solution();System.out.println(solution.myAtoi("2147483646"));System.out.println(solution.myAtoi("-91283472332"));System.out.println(solution.myAtoi("-42"));}static class Solution {public int myAtoi(String s) {char[] chars = s.trim().toCharArray();if (chars.length == 0) {return 0;}int sign = 1;int i = 0;if (!Character.isDigit(chars[0])) {if (chars[0] == '-') {sign = -1;} else if (chars[0] == '+') {sign = 1;} else {return 0;}i++;}long result = 0;while (i < chars.length && Character.isDigit(chars[i])) {switch (sign) {case 1: result = Math.min(result*10+chars[i] - '0',Integer.MAX_VALUE);break;case -1: result = Math.max(result*10+(chars[i] - '0')*sign, Integer.MIN_VALUE);break;default: return 0;}i++;}return (int) result;}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。