问题描述:
1. 问题涉及知识点.
- 字符翻转
2. 自己解法.
- 解法一:使用
string.charAr(index)
的方式反向的去字符.
import java.util.Scanner;
/**
* Create by tianchaoxiong on 18-4-9.
*/
public class HuaWei_12 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
String key = scanner.nextLine();
getResult(key);
}
}
private static void getResult(String key) {
int len = key.length();
for(int i=0;i<key.length();i++){
System.out.print(key.charAt(len-i-1));
}
}
}
- 解法二:使用
StringBuffer.reverse()
/StringBulder.reverse()
直接颠倒. //速度更快
package com.chaoxiong.niuke.huawei;
import java.util.Scanner;
/**
* Create by tianchaoxiong on 18-4-9.
*/
public class HuaWei_12_2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
String key = scanner.nextLine();
getResult(key);
}
}
private static void getResult(String key) {
System.out.println(new StringBuffer(key).reverse());
}
}
3. 优质答案.
null
4. 本题总结.
字符串的逆序,有两种方式
- 使用string.charAt(index)
反向取.
- 使用stringbuffer.reverse()
/StringBulder.reverse()
直接逆序.