Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eb6e8e4

Browse files
algorithms/src/main/java/ivanmarkovic/algorithms/recursion/ReverseString.java
1 parent a5cc1e2 commit eb6e8e4

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ivanmarkovic.algorithms.recursion;
2+
3+
public class ReverseString {
4+
5+
public static String reverse(String s) {
6+
7+
return helper(s, s.length() - 1);
8+
}
9+
10+
private static String helper(String s, int j) {
11+
if(j < 0)
12+
return "";
13+
else
14+
return String.valueOf(s.charAt(j)) + helper(s, j - 1);
15+
}
16+
17+
18+
public static String iterative(String s) {
19+
20+
char chars[] = new char[s.length()];
21+
int i = 0, j = chars.length - 1;
22+
while (i < j) {
23+
char tmp = chars[i];
24+
chars[i] = chars[j];
25+
chars[j] = tmp;
26+
i++;
27+
j--;
28+
}
29+
return new String(chars);
30+
}
31+
32+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /