Leetcode Reverse String 344

来源:互联网 发布:淘宝上外贸是正品吗 编辑:程序博客网 时间:2024/05/22 17:04

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) {
char[] c = s.toCharArray();
int len = s.length();
int left = 0, right = len - 1;
while(left < right){
char temp = c[left];
c[left] = c[right];
c[right] = temp;
left++;
right--;
}
String ans = String.valueOf(c);
return ans;
}
}

0 0