1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| func reverseString(s []byte) { for i := 0; i < len(s) >> 1; i++ { s[i], s[len(s) - 1 - i] = s[len(s) - 1 - i], s[i] } }
func reverseStr(s string, k int) string { tmp := []byte(s) i := 0 for i < len(tmp) { l := i r := i + k - 1 if r > len(s) - 1{ r = len(s) - 1 } for k := l; k < r; k++ { tmp[k], tmp[r] = tmp[r], tmp[k] r-- } i += 2 * k } return string(tmp) }
func reverseWords(s string) string { tmp := []string{} single := "" for i := 0; i < len(s); i++ { if s[i] == ' ' { if single != "" { tmp = append(tmp, single) } single = "" } else { single += string(s[i]) if i == len(s) - 1 { tmp = append(tmp, single) } } } reverseAll(tmp) res := "" for _, v := range tmp { res += (" " + string(v)) } return res[1:] }
func reverseAll(s []string) { for i := 0; i< len(s)>>1; i++ { s[i], s[len(s) - i -1] = s[len(s) - i -1], s[i] } }
func reverseWords(s string) string { tmp := strings.Split(s, " ") for i := 0; i < len(tmp); i++ { tmp[i] = reverSingle([]byte(tmp[i])) } return strings.Join(tmp, " ") }
func reverSingle(s []byte) string { for i := 0; i < len(s) >>1; i++ { s[i], s[len(s) - 1- i] = s[len(s) - 1- i], s[i] } return string(s) }
func reverseWords(s string) string { b :=[]byte(s) l :=0 for i,v :=range s{ if v==' '||i==len(s)-1{ r :=i-1 if i==len(s)-1{ r =i } for l<r{ b[l],b[r] =b[r],b[l] l++ r-- } l=i+1 } } return string(b) }
|