File tree Expand file tree Collapse file tree 1 file changed +10
-8
lines changed Expand file tree Collapse file tree 1 file changed +10
-8
lines changed Original file line number Diff line number Diff line change @@ -547,26 +547,28 @@ func reverseWords(s string) string {
547
547
b = b[:slowIndex]
548
548
}
549
549
// 2.反转整个字符串
550
- reverse (&b, 0 , len (b)- 1 )
550
+ reverse (b )
551
551
// 3.反转单个单词 i单词开始位置,j单词结束位置
552
552
i := 0
553
553
for i < len (b) {
554
554
j := i
555
555
for ; j < len (b) && b[j] != ' ' ; j++ {
556
556
}
557
- reverse (&b, i, j- 1 )
557
+ reverse (b[i:j] )
558
558
i = j
559
559
i++
560
560
}
561
561
return string (b)
562
562
}
563
563
564
- func reverse (b *[]byte , left , right int ) {
565
- for left < right {
566
- (*b)[left], (*b)[right] = (*b)[right], (*b)[left]
567
- left++
568
- right--
569
- }
564
+ func reverse (b []byte ) {
565
+ left := 0
566
+ right := len (b) - 1
567
+ for left < right {
568
+ b[left], b[right] = b[right], b[left]
569
+ left++
570
+ right--
571
+ }
570
572
}
571
573
```
572
574
You can’t perform that action at this time.
0 commit comments