344. Reverse String

来源:互联网 发布:php打印前一天时间 编辑:程序博客网 时间:2024/05/21 06:53

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 Solution {
    public String reverseString(String s) {
        if(s==null || s.length()==0){
            return s;
        }
        int left = 0;
        int right = s.length()-1;
        char[] sChar = s.toCharArray();
        while(left<right){
            char temp = sChar[left];
            sChar[left] = sChar[right];
            sChar[right] = temp;
            
            left++;
            right--;


        }
         return new String(sChar);
    }
}

0 0
原创粉丝点击