Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
题目标签:String
题目给了我们一个string,要求我们reverse string 并且返回。
可以利用two pointers 来 互换 左边的char 和右边的 char。具体看Code。
Java Solution:
Runtime beats 55.59%
完成日期:03/07/2017
关键词:two pointers
关键点:swap left and right chars
class Solution
{
public String reverseString(String s)
{
char [] st = s.toCharArray();
for(int i = 0; i < st.length / 2; i++)
{
int rc_index = st.length - i - 1;
// swap left and right characters
char lc = st[i];
st[i] = st[rc_index];
st[rc_index] = lc;
}
return new String(st);
}
}参考资料:n/a
LeetCode 题目列表 -LeetCode Questions List
题目来源:https://leetcode.com/