
本文共 1483 字,大约阅读时间需要 4 分钟。
������ Offer 58 - I. ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������"I am a student. "������������"student. a am I"���
������ 1���������: "the sky is blue"������: "blue is sky the"
������ 2���������: " hello world! "������: "world! hello"
������ 3���������: "a good example"������: "example good a"
������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
���������������������������������������������������
public class Solution { public String reverseWords(String s) { String[] res = s.trim().split(" "); StringBuilder sb = new StringBuilder(); for (int i = res.length - 1; i >= 0; i--) { if (!res[i].trim().isEmpty()) { sb.Append(res[i].trim()); if (i != 0) { sb.Append(" "); } } } return sb.ToString().trim(); } }
public static void main(String[] args) { new Solution().reverseWords(" hello world! ").trim(); }
发表评论
最新留言
关于作者
