NC100. 把字符串转换成整数(atoi)
描述
示例1
输入:
"82"
输出:
82
示例2
输入:
" -12 "
输出:
-12
说明:
去掉前后的空格,为-12示例3
输入:
"4396 clearlove"
输出:
4396
说明:
6后面的字符不属于有效的整数部分,去除,但是返回前面提取的有效部分示例4
输入:
"clearlove 4396"
输出:
0
示例5
输入:
"-987654321111"
输出:
-2147483648
C++ 解法, 执行用时: 2ms, 内存消耗: 432KB, 提交时间: 2022-02-09
class Solution { public: int StrToInt(string s) { int n = s.length(); // 去除前导空格 int index = 0; //遍历字符串的下标 while(index < n) { if (s[index] != ' ') break; index++; } if(index == n) //除了0就没有了 return 0; int sign = 1; // 处理第 1 个非空字符为正负符号 if(s[index] == '+') index++; else if(s[index] == '-') { sign = -1; index++; } int res = 0; while(index < n){ char c = s[index]; if(c < '0' || c > '9') //非数字跳出 break; //int型最大最小 if(res > INT_MAX / 10 || (res == INT_MAX / 10 && (c - '0') > INT_MAX % 10)) return INT_MAX; if(res < INT_MIN / 10 || (res == INT_MIN / 10 && (c - '0') < (-INT_MIN) % 10)) return INT_MIN; res = res * 10 + sign * (c - '0'); index++; } return res; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 480KB, 提交时间: 2022-02-09
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型 */ int StrToInt(string s) { // write code here int n = s.length(); int index = 0; while(index < n) { if(s[index]!= ' ') break; index ++; } bool flag = 1; if(is_op(s[index],flag)) index ++; long num = 0; while(index < n ) { if(!is_num(s[index]) ) break; else num = num *10 + s[index]- '0'; index ++; if(-num < INT_MIN) break; } if(!flag) num = -1 * num; num = num>INT_MAX? INT_MAX:(num< INT_MIN? INT_MIN:num); return num; } bool is_op( char c, bool & flag) { if(c == '+') return true; if(c == '-'){ flag = false; return true; } else return false; } bool is_num(char c) { if(c<='9' && c >='0') return true; else return false; } };
C++ 解法, 执行用时: 2ms, 内存消耗: 512KB, 提交时间: 2022-02-09
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型 */ bool avail(char ch) { if (ch == ' ') { return true; } if (ch == '-' || ch == '+') { return true; } if (ch >='0' && ch <= '9') { return true; } return false; } int StrToInt(string s) { bool foundSign = false; bool sign = true; bool isBegin = false; string strNum; for (const auto& ch : s) { if (!avail(ch)) { break; } if (isBegin && (ch > '9' || ch < '0')) { // 已经开始,但是遇到非数字 break; } if (ch == '-' || ch == '+') { isBegin = true; if (ch == '-') { sign = false; } } if (ch <= '9' && ch >= '0') { strNum += ch; isBegin = true; } } long res = 0; long maxOne = (long)INT32_MAX+1; for (const auto& ch : strNum) { res *= 10; res += ch - '0'; if (res >= maxOne) { res = maxOne; break; } } if (res == maxOne) { if (!sign) { return INT32_MIN; } return INT32_MAX; } if (sign) { return res; } else { return -res; } } };
C++ 解法, 执行用时: 3ms, 内存消耗: 408KB, 提交时间: 2022-07-25
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型 */ int StrToInt(string s) { // write code here int res = 0; int index = 0; int n = s.length(); while(index < n) { if(s[index] == ' ') index++; else break; } if(index == n) return 0; int sign = 1; if(s[index] == '+') index++; else if(s[index] == '-') { index++; sign = -1; } if(index == n) return 0; while(index < n) { char c = s[index]; if(c < '0' || c > '9') break; if(res > INT_MAX / 10 || (res == INT_MAX / 10 && (c - '0') > INT_MAX % 10)) return INT_MAX; if(res < INT_MIN / 10 || (res == INT_MIN / 10 && (c - '0') > -(INT_MIN % 10))) return INT_MIN; res = res * 10 + sign * (c - '0'); index++; } return res; } };
C++ 解法, 执行用时: 3ms, 内存消耗: 412KB, 提交时间: 2021-12-07
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型 */ int StrToInt(string s) { int n = s.length(); // 去除前导空格 int index = 0; //遍历字符串的下标 while(index < n) { if (s[index] != ' ') break; index++; } if(index == n) //除了0就没有了 return 0; int sign = 1; // 处理第 1 个非空字符为正负符号 if(s[index] == '+') index++; else if(s[index] == '-') { sign = -1; index++; } int res = 0; while(index < n){ char c = s[index]; if(c < '0' || c > '9') //非数字跳出 break; //int型最大最小 if(res > INT_MAX / 10 || (res == INT_MAX / 10 && (c - '0') > INT_MAX % 10)) return INT_MAX; if(res < INT_MIN / 10 || (res == INT_MIN / 10 && (c- '0') > -(INT_MIN % 10))) return INT_MIN; res = res * 10 + sign * (c - '0'); index++; } return res; } };