题目描述
描述
接受一个只包含小写字母的字符串,然后输出该字符串反转后的字符串。(字符串长度不超过1000)
输入描述:
输入一行,为一个只包含小写字母的字符串。
输出描述:
输出该字符串反转后的字符串。
题解
ACM 模式,其实和核心模式差不多的。
stringbuilder直接法:
运行时间:30ms
超过66.48% 用Java提交的代码
占用内存:10628KB
超过31.40%用Java提交的代码
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String a = in.nextLine();
String b = reverseString(a); // 调用核心模块
System.out.println(b);
}
}
public static String reverseString(String string) {
if (string == null || string.length() == 0)
return "";
StringBuilder sb = new StringBuilder(string);
sb.reverse();
return sb.toString();
}
}
复制代码
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END