https://leetcode.com/problems/adding-spaces-to-a-string/
// https://leetcode.com/problems/adding-spaces-to-a-string/
package main
import (
"fmt"
"strings"
)
func main() {
// s := "LeetcodeHelpsMeLearn"
// spaces := []int{8, 13, 15}
// s := "icodeinpython"
// spaces := []int{1, 5, 7, 9}
// s := "p"
// spaces := []int{0}
s := "ezixkFLjdbxrDowLVGYvdtBrguAAJVM"
spaces := []int{23}
addSpaces(s, spaces)
}
func addSpaces(s string, spaces []int) string {
if len(spaces) == 1 {
a := s[0:spaces[0]]
b := s[spaces[0]:]
return a + " " + b
}
var slice []string
for i := 1; i < len(spaces); i++ {
a := s[spaces[i-1]:spaces[i]]
slice = append(slice, a)
}
for i := 0; i < len(slice); i++ {
tmp := strings.Split(s, slice[i])
fmt.Println(tmp)
if i == 0 {
slice = append([]string{tmp[0]}, slice...)
} else if i == len(slice)-1 {
slice = append(slice, tmp[len(tmp)-1])
break
}
}
str := strings.Join(slice, " ")
return str
}
정답은 맞는것 같은데.. 저런식으로 들어올 때 무한루프 걸리는듯..
slice = append([]string{tmp[0]}, slice...) => prepend 로 쓸수 있는것만 알아두고.. pass..
func addSpaces(s string, spaces []int) string {
var ans bytes.Buffer
l := 0
for i:=0;i<len(spaces);i++{
ans.WriteString(s[l:spaces[i]] + " ")
l = spaces[i]
}
// ans.WriteString(s[l:len(s)])
ans.WriteString(s[l:])
return ans.String()
}
디스커션 보니까 이런식으로 푸넹..ㅠㅠ
'Algorithm > Go' 카테고리의 다른 글
[LeetCode] Remove Element (0) | 2022.01.31 |
---|---|
[LeetCode] Best Time to Buy and Sell Stock (0) | 2022.01.24 |
[LeetCode] Maximum Subarray (0) | 2022.01.10 |
[LeetCode] Longest Common Prefix (0) | 2022.01.09 |
[LeetCode] Longest Substring Without Repeating Characters (0) | 2022.01.03 |