-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAc.java
More file actions
41 lines (36 loc) · 961 Bytes
/
Ac.java
File metadata and controls
41 lines (36 loc) · 961 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com;
import java.util.Stack;
/**
* @author <a href="mailto:daiju3@crnet.com.hk">daiju3</a>
* @version 1.0.0
* @description
* @since 2018/6/29 16:31
*/
public class Ac {
public static String reverse(String s){
char [] str=s.toCharArray();
Stack<Character> stack=new Stack<Character>();
for(int i=0;i<str.length;i++){
stack.push(str[i]);
}
String reversed="";
for(int i=0;i<str.length;i++){
reversed+=stack.pop();
}
return reversed;
}
public static String reverse2(String s){
int length=s.length();
String reversed="";
for(int i=0;i<length;i++){
reversed=s.charAt(i)+reversed;
}
return reversed;
}
public static void main(String[] args) {
String a=reverse("abcdefg");
String b=reverse2("abcdefgh");
System.out.println(a);
System.out.println(b);
}
}