题解 | #反转字符串#
反转字符串
http://www.nowcoder.com/practice/c3a6afee325e472386a1c4eb1ef987f3
import java.util.*;
public class Solution {
/**
* 反转字符串
* @param str string字符串
* @return string字符串
*/
public String solve (String str) {
// write code here
if(str == null ||str.length() <= 1){
return str;
}
StringBuilder sb = new StringBuilder();
char[] data = str.toCharArray();
for(int i = data.length - 1;i>=0;i--){
sb.append(data[i]);
}
return sb.toString();
// char[] data = str.toCharArray();
// int i = 0,j = data.length - 1;
// char temp;
// while(i<j){
// temp = data[i];
// data[i] = data[j];
// data[j] = temp;
// i++;
// j--;
// }
// return new String(data);
}
}