LeetCode 344. Reverse String

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

 1 class Solution 
 2 {
 3     public String reverseString(String s) 
 4     {
 5         char [] st = s.toCharArray();
 6         
 7         for(int i = 0; i < st.length / 2; i++)
 8         {
 9             int rc_index = st.length - i - 1;
10             
11             // swap left and right characters
12             char lc = st[i];
13             st[i] = st[rc_index];
14             st[rc_index] = lc;
15             
16         }
17         
18         return new String(st);
19     }
20 }

参考资料:n/a

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

原文地址:https://www.cnblogs.com/jimmycheng/p/8689494.html