• 编写一个函数,其作用是将输入的字符串反转过来。

  • 示例 1:

输入: “hello”
输出: “olleh”
示例 2:

输入: “A man, a plan, a canal: Panama”
输出: “amanaP :lanac a ,nalp a ,nam A”

思路:最容易想到的就是遍历一遍然后把前后的位置元素互换,用一个t1变量临时存储内容;

public class Solution {
	
	public String reverseString(String s) {
		if(s.length()<1 || s==null) return s;
		char[] s2 = s.toCharArray();
		char t1 = 0;
		for(int i = 0;i<s2.length/2;i++) {
			t1 = s2[i];
			s2[i] = s2[s2.length-i-1];
			s2[s2.length-i-1] = t1;
		}
		for(int i = 0;i<s2.length;i++) {
			System.out.println(s2[i]);
		}
		return String.valueOf(s2);
    }
	public static void main(String[] args) {
		String s = "A man, a plan, a canal: Panama";
		Solution s1 = new Solution();
		s1.reverseString(s);
	}
}