https://leetcode.com/problems/reverse-string/description/
题目描述:Write a function that takes a string as input and returns the string reversed.
Example: Given s = “hello”, return “olleh”.
分析:将字符串逆序
public class Solution {
public String reverseString(String s) {
int n=s.length();
char[] res=new char[n];
char[] ori=s.toCharArray();
//创建新的字符串,逆序遍历旧字符串并存放
for(int i=0,j=n-1;i<n;i++,j--){
res[i]=ori[j];
}
return new String(res);
}
}
681

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



