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