LeetCode oj 344. Reverse String(字符串)

来源:互联网 发布:mac 当前用户路径 编辑:程序博客网 时间:2024/05/17 23:20

344. Reverse String

 
 My Submissions
  • Total Accepted: 90670
  • Total Submissions: 156010
  • Difficulty: Easy

Write a function that takes a string as input and returns the string reversed.

Example:
Given s = "hello", return "olleh".

Subscribe to see which companies asked this question

字符串翻转

直接翻转的话肯定超时,不然这道题就没有存在的意义了,从两边向中间翻转,可以节约一半的时间

public class Problem344 {public static String reverseString(String s) {char[] str = s.toCharArray();int len = s.length();char strTemp;for(int i=0;i<len/2;i++){strTemp = str[i];str[i] = str[len-1-i];str[len-1-i] = strTemp;}return new String(str);}public static void main(String [] args){System.out.println(reverseString("Heallo"));}}


0 0
原创粉丝点击