Write a function that reverses a string. The input string is given as an array of characters s.
You must do this by modifying the input array in-place with O(1) extra memory.
Example 1:
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Example 2:
Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
Constraints:
func reverseString(s []byte) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}
https://go.dev/play/p/qcDKdMx9uIq
func reverseString(s []byte) {
for i:=0 ; i<len(s)/2; i++ {
s[i], s[len(s)-1-i] = s[len(s)-1-i], s[i]
}
}
func reverseString(s []byte) {
i, j := 0, len(s) - 1
for i < j {
s[i], s[j] = s[j], s[i]
i++
j--
}
}
func reverseString(s []byte) {
var temp byte
for i:=0;i<len(s)/2;i++ {
temp = s[i]
s[i] = s[len(s) - i - 1]
s[len(s) - i - 1] = temp
}
}
func reverseString(s []byte) {
v := len(s)/2
for i := 0; i < v; i++ {
tmp := s[i]
s[i] = s[len(s)-1-i]
s[len(s)-1-i] = tmp
}
}
func reverseString(str []byte) {
size := len(str)
for i := 0; i < size / 2; i++ {
tpr := str[i]
str[i] = str[size - 1 - i]
str[size - 1 - i] = tpr
}
}
func reverseString(s []byte) {
var first, last int = 0, len(s) - 1
for ; first < last; {
s[first], s[last] = s[last], s[first]
first++
last--
}
}
func reverseString(s []byte) {
for i:=0; i<len(s)/2; i++ {
j := len(s)-i-1
temp := s[i]
s[i] = s[j]
s[j] = temp
}
}
[Go] Array and string - Two Sum II - Input Array Is Sorted (0) | 2022.04.12 |
---|---|
[Go] Array and String - Array Partition I (0) | 2022.04.10 |
[GO] Array and string - Two-pointer Technique - Scenario I (0) | 2022.04.09 |
[Go] Array and string - Longest Common Prefix (0) | 2022.04.08 |
[Go]Array and String - Implement strStr() (0) | 2022.04.04 |
댓글 영역