当前位置: 代码迷 >> 综合 >> Leetcode 8. 字符串转换整数 (atoi)(DAY 165)---- LeetCode 精选 TOP 面试题
  详细解决方案

Leetcode 8. 字符串转换整数 (atoi)(DAY 165)---- LeetCode 精选 TOP 面试题

热度:65   发布时间:2023-11-17 17:33:42.0

文章目录

    • 原题题目
    • 代码实现(首刷自解)
    • 代码实现(二刷自解 DAY 195 C++)
    • 代码实现(三刷自解 DAY 279 C++)
    • 代码实现(四刷自解 DAY 5 Golang)


原题题目


在这里插入图片描述


代码实现(首刷自解)


class Solution {
    
public:int myAtoi(string s) {
    bool neg = false,wrong = false,numend = false,dealnum = false;;long ret = 0;int numdeal = -1;for(int i=0;i<s.size();++i){
    char chr = s[i];if(chr != ' ' && chr != '-' && chr != '+'  && (chr > '9' || chr < '0')){
    wrong = true;break;}if(chr == ' ')  continue;else if(chr == '-' || chr == '+' || (chr >= '0' && chr <= '9')){
    if(chr == '-')  neg = true;numdeal = i;if(chr == '-' || chr == '+')    ++numdeal;break;}       }if(numdeal == -1)   wrong = true;for(int i=numdeal;!wrong && i<s.size();++i){
    char chr = s[i];if(chr < '0' || chr > '9')    {
    if(numend) wrong = true;else numend = true;break;}else{
    ret *= 10;ret += (chr - '0');if(ret > INT_MAX){
    if(neg) ret = INT_MIN;else    ret = INT_MAX;dealnum = true;break;}}}if(!dealnum) ret = (neg) ? -ret : ret; if(wrong)   ret = 0;return (int)ret;}
};

代码实现(二刷自解 DAY 195 C++)


class Solution {
    
public:int myAtoi(string s) {
    bool neg_flag = false,num_flag = false;int pos;long ret = 0;for(pos = 0;pos < s.size();++pos){
    char chr = s[pos];if(chr == ' ' || chr == '-' || chr == '+' || isdigit(chr)){
    if(chr == ' ') continue;if(isdigit(chr))    num_flag = true;else {
    if(chr == '-') neg_flag = true;++pos;}break;}else return 0;}long int_min = -static_cast<long>(INT_MIN);for( ;pos < s.size();++pos){
    char chr = s[pos];if(!isdigit(chr))   break;if(ret <= int_min){
    ret *= 10;ret += (chr - '0');}else  break;}if(neg_flag)    ret = -ret;ret = (ret <= INT_MIN ? INT_MIN : ret);ret = (ret >= INT_MAX ? INT_MAX : ret);return ret;}
};

代码实现(三刷自解 DAY 279 C++)


class Solution {
    public:int myAtoi(string s) {
    int pos = 0, size = s.size();bool neg = false;for (; pos < size; ++pos) {
    auto chr = s[pos];if (chr == ' ' || chr == '+' || chr == '-' || isdigit(chr)) {
    if (chr == ' ') {
    continue;} else {
    if (chr == '-' || chr == '+') {
    neg = (chr == '-');++pos;}break;}} else {
    return 0;}}if (pos == size)  return 0;int ret = 0;for (; pos < size; ++pos) {
    auto chr = s[pos];if (!isdigit(chr)) {
    break;}if (neg && abs((INT_MIN + (s[pos] - '0'))  / 10) < ret ||((INT_MAX - (s[pos] - '0')) / 10) < ret) {
    if (neg) {
    ret = INT_MIN;} else {
    ret = INT_MAX;}break;} else {
    ret *= 10;ret += s[pos] - '0';}}if (neg && ret != INT_MIN) ret = -ret;return ret;}
};

代码实现(四刷自解 DAY 5 Golang)


func myAtoi(s string) int {
    if len(s) == 0 {
    return 0}neg, pos := false, 0size := len(s)for pos < size && s[pos] == ' ' {
    pos++}if pos < size && (s[pos] == '+' || s[pos] == '-') {
    if s[pos] == '-' {
    neg = true}pos++}var ret int32 = 0const INT_MAX int32 = int32(^uint32(0) >> 1)const INT_MIN int32 = ^INT_MAXfor pos < size {
    if s[pos] < '0' || s[pos] > '9' {
    break}num := int32(s[pos] - '0')if neg == false && (INT_MAX - num) / 10 < ret {
    ret = INT_MAXbreak}if neg == true && (INT_MIN + num) / 10 > ret {
    ret = INT_MINbreak}ret *= 10if neg {
    ret -= num} else {
    ret += num}pos++}return int(ret)
}