Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Nov 26, 2023
1 parent 34a1bc0 commit 1b4004f
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions leetcode/344.ReverseString/reverseString.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"bytes"
"fmt"
)

func reverseString(s []byte) {
for i, j := 0, len(s)-1; i < j; i++ {
s[i], s[j] = s[j], s[i]
j--
}
}

func reverseString1(s []byte) {
for i := 0; i < len(s)/2; i++ {
j := len(s) - i - 1
s[i], s[j] = s[j], s[i]
}
}

func reverseString2(s []byte) {
i := 0
j := len(s) - i - 1

for i < j {
s[i], s[j] = s[j], s[i]
i++
j--
}
}

func reverseString3(s []byte) {
for i, j := 0, len(s)-1; i < j; j = len(s) - i - 1 {
s[i], s[j] = s[j], s[i]
i++
j--
}
}

func reverseString4(s []byte) {
b := bytes.Runes(s)
for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 {
b[i], b[j] = b[j], b[i]
}
copy(s, string(b))
}

func main() {
s := []byte{'h', 'e', 'l', 'l', 'o'}

reverseString3(s)

fmt.Println(string(s))
}

0 comments on commit 1b4004f

Please sign in to comment.