p344

LeetCode P344 Reverse String 题解

1.题目:

Write a function that takes a string as input and returns the string reversed.

Example:
Given s = “hello”, return “olleh”.

Subscribe to see which companies asked this question

题意:输入一个字符串返回它反转之后的字符串

2.代码


[title] [] [url] [link text]
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
 
package leetcode;

import java.util.Scanner;

/**
*
* @author ZhangMengRou
*
*/
public class p344 {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String a = in.nextLine();
String ans = new String();
ans = reverseString(a);
System.out.print(ans);
}

public static String reverseString(String s) {

StringBuilder bu = new StringBuilder();
StringBuilder ans = new StringBuilder();
bu.append(s);
int len = bu.length();
for (int i = len - 1; i > -1; i--) {
ans.append(s.charAt(i));
}
// System.out.print(ans);
return new String(ans);
}

}


3.一些总结:了解输入输出