344. Reverse String

来源:互联网 发布:淘宝卖家怎么添加商品 编辑:程序博客网 时间:2024/05/18 01:35

344. Reverse String

   

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

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

//把字符串进行翻转 利用i、j两个数进行标记下标

public class Solution {
    public String reverseString(String s) {
        char[] a=s.toCharArray();
        int i=0;
        int j=s.length()-1;
        while(i<=j){
            char temp=a[j];
            a[j]=a[i];
            a[i]=temp;
            i++;
            j--;
        }
        return new String(a);
    }
}

0 0
原创粉丝点击