题目地址:https://leetcode.com/problems/reverse-string/open in new window
Total Accepted: 11014 Total Submissions: 18864 Difficulty: Easy
题目描述
Write a function that takes a string as input and returns the string reversed.
Example 1:
Input: "hello"
Output: "olleh"
Example 2:
Input: "A man, a plan, a canal: Panama"
Output: "amanaP :lanac a ,nalp a ,nam A"
题目大意
新构建字符串
字符串按位翻转:
public class Solution {
public String reverseString(String s) {
StringBuffer answer=new StringBuffer("");
int tail=s.length()-1;
for(int i=tail;i>=0;i--){
answer.append(s.charAt(i));
}
return answer.toString();
}
}
1 2 3 4 5 6 7 8 9 10 11
AC:6ms
python支持切片进行翻转,所以代码只有一行。
class Solution:
def reverseString(self, s):
"""
:type s: str
:rtype: str
"""
return s[::-1]
1 2 3 4 5 6 7
原地翻转
转换为字符串数组后,in-place翻转。下面的这个java解法返回值是个新的字符串,但是作为结果应该不计算空间复杂度之内。
java代码:
public class Solution {
public String reverseString(String s) {
char[] chars=s.toCharArray();
for(int i=0;i<chars.length/2;i++){
char temp=chars[i];
chars[i]=chars[chars.length-1-i];
chars[chars.length-1-i]=temp;
}
return new String(chars);
}
}
1 2 3 4 5 6 7 8 9 10 11 12
AC:3ms
DDKK.COM 弟弟快看-教程,程序员编程资料站,版权归原作者所有
本文经作者:负雪明烛 授权发布,任何组织或个人未经作者授权不得转发