dongzhi9457 2019-01-06 08:20
浏览 157
已采纳

为什么leetcode说我的atoi答案不正确? 真的不正确吗? 还是leetcode中有错误

I am doing the atoi problem in leetcode and I submitted my code below which isn't too important. I am wondering if it is a valid failure leetcode gave me. It seems like my code is doing the right thing.

Here is the problem description: enter image description here

leet code problem Here is the code:

const (
    MaxInt32 = 1<<31 - 1
    MinInt32 = -1 << 31
)

func myAtoi(str string) int {
    if len(str) < 1 {
        return 0
    }
    // count to keep track of the number of digits
    count := 0
    // container for digits
    values := make([]rune, 0)
    // constant we are going to need
    minus := "-"
    plus := "+"
    lastWasPrefix := false
    // is the number negative or lead with dash
    negative := false

    clean := strings.TrimSpace(str)
    for _, char := range clean {
        isNumber := unicode.IsNumber(char)
        isMinus := string(char) == minus
        isPlus := string(char) == plus
        isPrefix := isMinus || isPlus
        // checking for two prefixes following eachother
        if isPrefix && lastWasPrefix {
            return 0
        }
        if isPrefix {
            lastWasPrefix = true
        }
        curLen := len(values)
        if !isNumber && !isPrefix && curLen == 0 {
            return 0
        }

        if !isNumber && !isPrefix && curLen != 0 {
            break
        }

        if isMinus {
            negative = true
            continue
        }

        if isNumber {
            // add value in order and inc. count
            values = append(values, char)
            count++
        }
    }

    postLen := len(values)

    if postLen == 0 {
        return 0
    }

    multiplier := int32(1)
    ten := int32(10)
    total := int32(0)
    for i := postLen - 1; i >= 0; i-- {
        diff := MaxInt32 - total
        added := CharToNum(values[i]) * multiplier
        // added will be zero if we overflow the int
        if added > diff || added < 0 {
            return MinInt32
        }
        total += added
        multiplier *= ten
    }

    if negative {
        return int(total * int32(-1))
    } else {
        return int(total)
    }
}

/**
a rune is a uni code char so we need to conver from unicode int to digit
 */
func CharToNum(r rune) (int32) {
    for i := 48; i <= 57; i++ {
        if int(r) == i {
            return int32(r) - 48
        }
    }
    return -1
}

Any help understanding this error would be much appreciated. I don't want any help with the algorithm. Is this a valid error or not?

  • 写回答

2条回答 默认 最新

  • douhuang3740 2019-01-06 08:26
    关注

    The specification is ambiguous "if the numerical value is out of the range of representable values INT_MAX or INT_MIN is retuned". The authors had in mind to return the value matching in sign but this is not clearly stated.

    So I would say when you return INT_MIN for a number that is larger than INT_MAX this could be considered correct (although it is somewhat illogical).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考