您的位置:首页 > 其它

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

2016-04-30 12:04 337 查看
原题网址: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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: