@XQF
2018-03-07T14:59:03.000000Z
字数 1057
阅读 913
数据结构与算法
I love you------>uoy evol I
public class Solution {public String reserve(String string) {if(string=null){return null;}char[] chars = string.toCharArray();for (int i = 0; i < chars.length / 2; i++) {swap(chars, i, chars.length - 1 - i);}return new String(chars);}public void swap(char[] chars, int i, int j) {char temp = chars[i];chars[i] = chars[j];chars[j] = temp;}public static void main(String[] args) {Solution solution = new Solution();String string = "I love you";System.out.println(solution.reserve(string));}}
I love you -->you love I
换汤不换药
public class Solution {public String reserve(String string) {if (string == null || string.length() == 0) {return null;}String[] strs = string.split(" ");for (int i = 0; i < strs.length / 2; i++) {swap(strs, i, strs.length - 1 - i);}StringBuffer sb = new StringBuffer();for (int i = 0; i < strs.length; i++) {sb.append(strs[i] + " ");}return sb.toString();}public void swap(String[] strs, int i, int j) {String temp = strs[i];strs[i] = strs[j];strs[j] = temp;}public static void main(String[] args) {Solution solution = new Solution();String string = "I love you";System.out.println(solution.reserve(string));}}
