Skip to content

Instantly share code, notes, and snippets.

@mumunuu
Last active January 30, 2021 04:24
Show Gist options
  • Save mumunuu/43717369bc876fb702b64d4bbad828b5 to your computer and use it in GitHub Desktop.
Save mumunuu/43717369bc876fb702b64d4bbad828b5 to your computer and use it in GitHub Desktop.
algorithm(leetcode) Count and Say
func countAndSay(n int) string {
if n == 1 {
return "1"
}
base := "1"
baseTmpArr := make([][]string, 0)
for i := 2; i <= n; i++ { //n회 반복
curVal := ""
tmpArr := make([]string, 0)
for i, v := range base {
if i == 0 {
tmpArr = append(tmpArr, string(v)) //처음엔 무조건 넣어줌
curVal = string(v)
} else if curVal != string(v) {
baseTmpArr = append(baseTmpArr, tmpArr) //이제까지 했던걸 넣어준다.
tmpArr = make([]string, 0) //초기화 해줌
tmpArr = append(tmpArr, string(v)) //넣어줌
curVal = string(v) //현재 값으로 바꿔줌
} else {
tmpArr = append(tmpArr, string(v)) //넣어줌
}
}
if len(tmpArr) != 0 {
baseTmpArr = append(baseTmpArr, tmpArr) //마지막에 독립된 숫자 처리
}
//e.g) 3일때 [[1,1]]
tmpBase := ""
for _, v := range baseTmpArr {
tmpBase += strconv.Itoa(len(v)) + v[0]
}
base = tmpBase
baseTmpArr = make([][]string, 0) //임시 배열 초기화
}
return base
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment