9 I. 翻转单词顺序
发布日期:2021-05-10 01:34:59 浏览次数:15 分类:精选文章

本文共 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"

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������������������������������������

  • ������������������������������������������
  • ������split(" ")������������������������������������������������
  • ������������StringBuilder������
  • ���������������������������������������������������������������StringBuilder���
  • ������������������������������������������������������������������������
  • ���������StringBuilder���������������������������������������������������
  • 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(); }

    上一篇:10 II. 左旋转字符串
    下一篇:8 第一个只出现一次的字符

    发表评论

    最新留言

    逛到本站,mark一下
    [***.202.152.39]2025年04月01日 12时15分44秒