额,这题很明显就可用双指针,感觉没考察点啊,今天主要在实战pprof,可总结的不多,不想写别的了,多po俩实现
一次迭代string累加,时间很慢,原因是string。
func compressString(s string) string { if len(s) == 0 { return s } ans := "" ch := s[0] cnt := 1 for i := 1; i < len(s); i++ { if ch == s[i] { cnt++ } else { ans += string(ch) + strconv.Itoa(cnt) cnt = 1 ch = s[i] } } ans += string(ch) + strconv.Itoa(cnt) if len(ans) >= len(s) { return s } return ans }
其实我对go的一些小细节还不太熟悉
func method2(s string) string { if s == "" { return "" } var stringBuilder strings.Builder curr := s[0] curLen := 1 for i := 0; i < len(s); i++ { if s[i] == curr { curLen++ } else { stringBuilder.WriteByte(curr) stringBuilder.WriteString(strconv.Itoa(curLen)) curr = s[i] curLen = 1 } } stringBuilder.WriteByte(curr) stringBuilder.WriteString(strconv.Itoa(curLen)) if stringBuilder.Len() >= len(s) { return s } return stringBuilder.String() }
最后,正常人的思路,双指针
func compressString1(S string) string { if len(S) <= 1 { return S } res := "" slow, quick := 0, 0 for quick < len(S) { if S[quick] != S[slow] { res += string(S[slow]) + strconv.Itoa(quick-slow) slow = quick } quick++ } res += string(S[slow]) + strconv.Itoa(quick-slow) if len(res) >= len(S) { return S } return res }
end