Skip to content

Latest commit

 

History

History
54 lines (47 loc) · 1.52 KB

816. 模糊坐标.md

File metadata and controls

54 lines (47 loc) · 1.52 KB

816. 模糊坐标

题目传送门

点击这里

解题思路

这道题是遍历枚举,设定条件做判断的类型题,和很多字符合法、数字合法的题类似,具体的判断思路见代码注释。

代码

// 判断小数点位置
func getPos(s string) (pos []string) {
    // 如果s本身是0,或者首位不为0,可以直接将s添加。
    if s[0] != '0' || s == "0" {
        pos = append(pos, s)
    }
    // 从第二位开始
    for p := 1; p < len(s); p++ {
        // 如果p的位置大于1,且首字母是0,则不能在后面添加小数点,会不合法,比如 '01234',不可以在'1'之后添加小数点
        // 且如果数字的结尾是0的话,也不能在任何位置添加小数点,会不合法,比如 '12340',只能当作一体
        if p != 1 && s[0] == '0' || s[len(s)-1] == '0' {
            continue
        }
        pos = append(pos, s[:p]+"."+s[p:])
    }
    return
}

func ambiguousCoordinates(s string) (res []string) {
    // 去掉括号
    n := len(s) - 2
    s = s[1 : len(s)-1]
    for l := 1; l < n; l++ {
        // 使用逗号分割成lt和rt两部分 
        lt := getPos(s[:l])
        if len(lt) == 0 {
            continue
        }
        rt := getPos(s[l:])
        if len(rt) == 0 {
            continue
        }
        for _, i := range lt {
            for _, j := range rt {
                res = append(res, "("+i+", "+j+")")
            }
        }
    }
    return
}