{ "type": "code_options", "author": "csdn.net", "source": "solution.md", "exercise_id": "801e487f54b446efb2aee528636f5484", "keywords": "字符串", "title": "字符串转换整数 (atoi)", "desc": [ { "content": "\n
\n

请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数)。

", "language": "markdown" }, { "content": "\n

函数 myAtoi(string s) 的算法如下:

", "language": "markdown" }, { "content": "\n", "language": "markdown" }, { "content": "\n

注意:

", "language": "markdown" }, { "content": "\n", "language": "markdown" }, { "content": "\n

 

", "language": "markdown" }, { "content": "\n

示例 1:

", "language": "markdown" }, { "content": "\n
输入:s = \"42\"\n输出:42\n解释:加粗的字符串为已经读入的字符,插入符号是当前读取的字符。\n第 1 步:\"42\"(当前没有读入字符,因为没有前导空格)\n^\n第 2 步:\"42\"(当前没有读入字符,因为这里不存在 '-' 或者 '+')\n^\n第 3 步:\"42\"(读入 \"42\")\n^\n解析得到整数 42 。\n由于 \"42\" 在范围 [-231, 231 - 1] 内,最终结果为 42 。
", "language": "markdown" }, { "content": "\n

示例 2:

", "language": "markdown" }, { "content": "\n
输入:s = \"   -42\"\n输出:-42\n解释:\n第 1 步:\"   -42\"(读入前导空格,但忽视掉)\n^\n第 2 步:\"   -42\"(读入 '-' 字符,所以结果应该是负数)\n^\n第 3 步:\"   -42\"(读入 \"42\")\n^\n解析得到整数 -42 。\n由于 \"-42\" 在范围 [-231, 231 - 1] 内,最终结果为 -42 。\n
", "language": "markdown" }, { "content": "\n

示例 3:

", "language": "markdown" }, { "content": "\n
输入:s = \"4193 with words\"\n输出:4193\n解释:\n第 1 步:\"4193 with words\"(当前没有读入字符,因为没有前导空格)\n^\n第 2 步:\"4193 with words\"(当前没有读入字符,因为这里不存在 '-' 或者 '+')\n^\n第 3 步:\"4193 with words\"(读入 \"4193\";由于下一个字符不是一个数字,所以读入停止)\n^\n解析得到整数 4193 。\n由于 \"4193\" 在范围 [-231, 231 - 1] 内,最终结果为 4193 。\n
", "language": "markdown" }, { "content": "\n

示例 4:

", "language": "markdown" }, { "content": "\n
输入:s = \"words and 987\"\n输出:0\n解释:\n第 1 步:\"words and 987\"(当前没有读入字符,因为没有前导空格)\n^\n第 2 步:\"words and 987\"(当前没有读入字符,因为这里不存在 '-' 或者 '+')\n^\n第 3 步:\"words and 987\"(由于当前字符 'w' 不是一个数字,所以读入停止)\n^\n解析得到整数 0 ,因为没有读入任何数字。\n由于 0 在范围 [-231, 231 - 1] 内,最终结果为 0 。
", "language": "markdown" }, { "content": "\n

示例 5:

", "language": "markdown" }, { "content": "\n
输入:s = \"-91283472332\"\n输出:-2147483648\n解释:\n第 1 步:\"-91283472332\"(当前没有读入字符,因为没有前导空格)\n^\n第 2 步:\"-91283472332\"(读入 '-' 字符,所以结果应该是负数)\n^\n第 3 步:\"-91283472332\"(读入 \"91283472332\")\n^\n解析得到整数 -91283472332 。\n由于 -91283472332 小于范围 [-231, 231 - 1] 的下界,最终结果被截断为 -231 = -2147483648 。
", "language": "markdown" }, { "content": "\n

 

", "language": "markdown" }, { "content": "\n

提示:

", "language": "markdown" }, { "content": "\n\n
", "language": "markdown" } ], "answer": [ { "content": "", "language": "java" } ], "prepared": [ [ { "content": "", "language": "java" } ], [ { "content": "", "language": "java" } ], [ { "content": "", "language": "java" } ] ], "template": { "content": "class Solution {\n\tpublic int myAtoi(String s) {\n\t\tlong y = 0;\n\t\tint i = 0;\n\t\tboolean w = false;\n\t\tboolean sign = false;\n\t\tint offset = 0;\n\t\tchar[] ints = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };\n\t\twhile (i < s.length()) {\n\t\t\tchar c = s.charAt(i);\n\t\t\tboolean isSign = false;\n\t\t\tif (w == false && c != ' ') {\n\t\t\t\tw = true;\n\t\t\t\tif (c == '-') {\n\t\t\t\t\tsign = true;\n\t\t\t\t\tisSign = true;\n\t\t\t\t}\n\t\t\t\tif (c == '+') {\n\t\t\t\t\tisSign = true;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (w && (!isSign)) {\n\t\t\t\tint v = Arrays.binarySearch(ints, c);\n\t\t\t\tif (v < 0) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\ty = y * 10 + v;\n\t\t\t\tif (y > 0x7FFFFFFF) {\n\t\t\t\t\ty = 0x7FFFFFFF;\n\t\t\t\t\toffset = 1;\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\ti++;\n\t\t}\n\t\treturn sign ? -(int) (y + offset) : (int) y;\n\t}\n}", "language": "java" }, "node_id": "dailycode-cba3b9fdcdbc447981e98ac7d67b82c6", "license": "csdn.net", "created_at": 1637894159, "topic_link": "https://bbs.csdn.net/topics/600470794" }