golang學習day4

作者: SecondRun (雨夜琴聲)   2024-01-26 11:14:51
2138. Divide a String Into Groups of Size k
A string s can be partitioned into groups of size k using the following procedure:
The first group consists of the first k characters of the string, the second group consists of the next k characters of the string, and so on. Each character can be a part of exactly one group.
For the last group, if the string does not have k characters remaining, a character fill is used to complete the group.
以下golang code
func divideString(s string, k int, fill byte) []string {
result := []string{}
for i := 0; i < len(s); i += k {
if i+k <= len(s) {
result = append(result, s[i:i+k])
continue
}
str := s[i:len(s)]
for len(str) < k {
str += string(fill)
}
result = append(result, str)
}
return result
}
每天記憶都會重置
都要在append這裡卡一下==
然後if else的排版錯了(換個行之類)就會直接編譯不過
有點神奇
然後有辦法在宣告一個 指定最大長度,但長度是0的slice嗎
作者: sustainer123 (caster)   2024-01-26 11:20:00
大師
作者: CP3isgood (3345678)   2024-01-26 11:21:00
要固定長度只能用array吧
作者: SecondRun (雨夜琴聲)   2024-01-26 11:27:00
:O

Links booklink

Contact Us: admin [ a t ] ucptt.com