Zheaoli / do-something-right

MIT License
37 stars 3 forks source link

2022-05-09 #232

Open Zheaoli opened 2 years ago

Zheaoli commented 2 years ago

2022-05-09

SaraadKun commented 2 years ago

942. 增减字符串匹配

image

func diStringMatch(s string) []int {
    lo, hi, ans := 0, len(s), make([]int, len(s) + 1)
    for i, ch := range s {
        if (ch == 'I') {
            ans[i] = lo 
            lo++
        } else {
            ans[i] = hi
            hi--
        }
    }
    ans[len(s)] = lo
    return ans
}

WeChat: Saraad

MILIFIRE commented 2 years ago

carbon (10) carbon (9)

dreamhunter2333 commented 2 years ago
package main

/*
 * @lc app=leetcode.cn id=942 lang=golang
 *
 * [942] 增减字符串匹配
 */

// @lc code=start
func diStringMatch(s string) []int {
    res := make([]int, 0)
    start := 0
    end := len(s)
    for _, c := range s {
        if c == 'I' {
            res = append(res, start)
            start++
        } else {
            res = append(res, end)
            end--
        }
    }
    res = append(res, end)
    return res
}

// func main() {
//  fmt.Println(diStringMatch("IDID"))
// }
// @lc code=end

微信id: 而我撑伞 来自 vscode 插件