当前位置: 代码迷 >> 综合 >> 344. Reverse String LeetCode 解题
  详细解决方案

344. Reverse String LeetCode 解题

热度:3   发布时间:2023-12-10 03:00:41.0

344. Reverse String

Difficulty: Easy

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[] sArray = s.toCharArray();int length = sArray.length;     for (int i = 0; i < length/2; i ++) {char temp = sArray[i];int position = length - 1 - i;sArray[i] = sArray[position];sArray[position] = temp;}return new String(sArray);}
}
 总觉得这个代码效率有点低,如果有更快的解决方法,欢迎交流啊。
  相关解决方案