344. Reverse String

来源:互联网 发布:深度linux 密码忘记 编辑:程序博客网 时间:2024/06/11 04:33

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.


Java Code:

public class Solution {    public String reverseString(String s) {        char[] sc = s.toCharArray();                int lsd = 0;        int rsd = sc.length - 1;                while (lsd < rsd) {            char c = sc[lsd];            sc[lsd] = sc[rsd];            sc[rsd] = c;            lsd++;            rsd--;        }                return new String(sc);    }}


0 0
原创粉丝点击