原题网址:https://leetcode.com/problems/reverse-string/
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) {
if (s == null) return null;
char[] sa = s.toCharArray();
char[] r = new char[sa.length];
for(int i=0; i<sa.length; i++) r[i] = sa[sa.length-1-i];
return new String(r);
}
}

本文介绍了一种使用Java实现字符串反转的方法。通过循环遍历字符数组并将其逆序存储到新数组中,最终返回反转后的字符串。例如输入hello,输出为olleh。
&spm=1001.2101.3001.5002&articleId=51286005&d=1&t=3&u=6a9543def2f943dd98992dd4dc34921f)
435

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



