344. Reverse String
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
//把字符串进行翻转 利用i、j两个数进行标记下标
public class Solution {
public String reverseString(String s) {
char[] a=s.toCharArray();
int i=0;
int j=s.length()-1;
while(i<=j){
char temp=a[j];
a[j]=a[i];
a[i]=temp;
i++;
j--;
}
return new String(a);
}
}
本文介绍了一种通过使用i、j两个数进行标记下标的简单方法来实现字符串翻转的算法。具体步骤包括将输入字符串转换为字符数组,设置i、j两个指针分别指向数组的首尾,然后通过循环交换i、j位置的字符,直至i超过j,最终返回翻转后的字符串。
735

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



