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