344. Reverse String

来源:互联网 发布:高晓松酒驾 知乎 编辑:程序博客网 时间:2024/05/16 13:05

Problem Statement

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

Example 1:

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

Thinking

反转单词。

Solution

class Solution {    public String reverseString(String s) {        char[] s1 = s.toCharArray();        int len = s1.length - 1;        int hed = 0;        while(hed < len){            char x = s1[hed];            s1[hed] = s1[len];            s1[len] = x;            hed++;len--;        }        return new String(s1);    }}