lintcodeleetcode由易至难第8题:Reverse String

xiaoxiao2021-02-28  104

Problem:

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

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

Code1:

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

Code2:

public class Solution { public String reverseString(String s) { StringBuilder s5 = new StringBuilder(s); return s5.reverse().toString(); } }

转载请注明原文地址: https://www.6miu.com/read-60165.html

最新回复(0)