public class Solution { public String reverseWords(String s) { String ans=reverse(s); String s2[]=ans.split("\\s+"); StringBuffer sb=new StringBuffer(); for(int i=0;i<s2.length;i++) { sb.append(reverse(s2[i])+" "); } return sb.toString().trim(); } private String reverse(String s) { int len=s.length(); char c[]=s.toCharArray(); for(int i=0;i<len/2;i++) { char tem=c[i]; c[i]=c[len-1-i]; c[len-1-i]=tem; } return String.valueOf(c); } }
逆转字符串leetcode
时间: 2024-11-12 16:45:51