文章目录
- 随便聊聊时间
- 题目:按分隔符拆分字符串
- 题目描述
- 代码与解题思路
 
随便聊聊时间

 LeetCode?启动!!!
时隔半个月,LeetCode 每日一题重新开张,寒假学习,正式开始
题目:按分隔符拆分字符串
题目链接:2788. 按分隔符拆分字符串
题目描述

代码与解题思路
可以直接手动模拟:
func splitWordsBySeparator(words []string, separator byte) (ans []string) {
    for i := 0; i < len(words); i++ {
        s := ""
        for j := 0; j < len(words[i]); j++ {
            if words[i][j] == separator {
                if s != "" {
                    ans = append(ans, s)
                    s = ""
                }
                continue
            }
            s += string(words[i][j])
        }
        if s != "" {
            ans = append(ans, s)
        }
    }
    return ans
}
也可以直接调库:
func splitWordsBySeparator(words []string, separator byte) (ans []string) {
    for _, str := range words {
        // string([]byte{separator})
        s := strings.Split(str, string(separator))
        for _, v := range s {
            if len(v) > 0 {
                ans = append(ans, v)
            }
        }
    }
    return ans
}
看官方题解学到新知识:
我原先使用的:string(separator),可以将 byte 类型的一个字符转换成 string 类型,官方的那个更高级一点,他用的:string([ ]byte{separator}),如果分隔符不止一个字符的话,我们可以给这个 byte 切片手动追加字符



















