Skip to content

Commit d65d5d9

Browse files
committed
Create 0344-reverse-string.kt
1 parent 3544142 commit d65d5d9

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Diff for: kotlin/0344-reverse-string.kt

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Using 2 variables for the pointers
3+
*/
4+
class Solution {
5+
fun reverseString(s: CharArray): Unit {
6+
var low = 0
7+
var high = s.size-1
8+
while(low < high){
9+
val temp = s[high]
10+
s[high] = s[low]
11+
s[low] = temp
12+
low++
13+
high--
14+
}
15+
}
16+
}
17+
18+
/*
19+
* Using one variable for the pointers
20+
*/
21+
class Solution {
22+
fun reverseString(s: CharArray): Unit {
23+
val size = s.size
24+
for(i in 0 until size/2){
25+
val temp = s[i]
26+
s[i] = s[size-1-i]
27+
s[size-1-i] = temp
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)