forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path_344.java
More file actions
27 lines (23 loc) · 647 Bytes
/
_344.java
File metadata and controls
27 lines (23 loc) · 647 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.fishercoder.solutions;
/**
* 344. Reverse String
*
* Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".*/
public class _344 {
public String reverseString_cheating(String s) {
return new StringBuilder(s).reverse().toString();
}
public String reverseString(String s) {
int i = 0;
int j = s.length() - 1;
char[] chars = s.toCharArray();
while (i < j) {
char temp = chars[i];
chars[i++] = chars[j];
chars[j--] = temp;
}
return new String(chars);
}
}