Write a function that takes a string as input and returns the string reversed.
Example:
Given s = “hello”, return “olleh”.
这道题简单到爆了,给两个解,一个是我刚开始做题时候的,一个是一个月后的(是不是感觉提升很多)
类似题目:https://blog.jing.do/4772
public class Solution { public String reverseString(String s) { char[] sc = s.toCharArray(); char[] res = new char[sc.length]; int l=0; for(int i=sc.length-1; i>=0;i--){ res[l] = sc[i]; l++; } return new String(res); } }
public class Solution { public String reverseString(String s) { char[] word = s.toCharArray(); int i=0; int l=word.length -1; while(i<l){ char temp = word[i]; word[i] = word[l]; word[l] = temp; i++; l--; } return new String(word); } }