请实现一个算法,在不使用额外数据结构和储存空间的情况下,翻转一个给定的字符串(可以使用单个过程变量)。
给定一个string iniString,请返回一个string,为翻转后的字符串。保证字符串的长度小于等于5000。
测试样例:
"This is nowcoder"
返回:"redocwon si sihT"
注:字符串的处理 较简单哈
public class Reverse { public String reverseString(String iniString) { // write code here int start = 0; int end = iniString.length()-1; char [] ch = iniString.toCharArray(); return reverseString(ch,start,end); } public String reverseString(char [] ch, int start, int end){ if(start < end) { char tmp; tmp = ch[start]; ch[start] = ch[end]; ch[end] = tmp; reverseString(ch,start+1,end-1); } return new String(ch); } public static void main(String[] args) { Reverse rs = new Reverse(); System.out.println(rs.reverseString("This is nowcoder")); } }
&spm=1001.2101.3001.5002&articleId=47908083&d=1&t=3&u=b08be81781ad46b2add820f4f04c32d3)
118

被折叠的 条评论
为什么被折叠?



