Write a function that takes a string as input and returns the string reversed.
Example:
Given s = “hello”, return “olleh”.
这道题简单到爆了,给两个解,一个是我刚开始做题时候的,一个是一个月后的(是不是感觉提升很多)
类似题目:https://blog.jing.do/4772
public class Solution { public String reverseString(String s) { char[] sc = s.toCharArray(); char[] res = new char[sc.length]; int l=0; for(int i=sc.length-1; i>=0;i--){ res[l] = sc[i]; l++; } return new String(res); } }
public class Solution { public String reverseString(String s) { char[] word = s.toCharArray(); int i=0; int l=word.length -1; while(i<l){ char temp = word[i]; word[i] = word[l]; word[l] = temp; i++; l--; } return new String(word); } }
[参与人数: 0 平均分: 0/5]
版权申明:本站原创文章皆遵循“署名-非商业性使用-相同方式共享 3.0 (CC BY-NC-SA 3.0)”。转载请保留以下标注:原文来源:《LeetCode – 344. Reverse String》
发表评论