LeetCode 344. Reverse String(字符串翻转)

来源:互联网 发布:db2和sql server 编辑:程序博客网 时间:2024/05/17 08:21

原题网址:https://leetcode.com/problems/reverse-string/

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

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

方法:循环。

public class Solution {    public String reverseString(String s) {        if (s == null) return null;        char[] sa = s.toCharArray();        char[] r = new char[sa.length];        for(int i=0; i<sa.length; i++) r[i] = sa[sa.length-1-i];        return new String(r);    }}

0 0