LCR 181. 字符串中的单词反转

解题思路:

java 复制代码
class Solution {
    public String reverseMessage(String message) {
        message = message.trim();                               // 删除首尾空格
        int j = message.length() - 1, i = j;
        StringBuilder res = new StringBuilder();
        while (i >= 0) {
            while (i >= 0 && message.charAt(i) != ' ') i--;     // 搜索首个空格
            res.append(message.substring(i + 1, j + 1) + " ");  // 添加单词
            while (i >= 0 && message.charAt(i) == ' ') i--;     // 跳过单词间空格
            j = i;                                              // j 指向下个单词的尾字符
        }
        return res.toString().trim();                           // 转化为字符串并返回
    }
}
相关推荐
Eason_LYC14 小时前
【GetShell 实战】CVE-2026-34486 Tomcat 加密拦截器绕过:从漏洞验证到反弹 Shell 全流程
java·渗透测试·tomcat·java反序列化·rce·远程代码执行漏洞·cve-2026-34486
qq_25183645714 小时前
基于java 税务管理系统设计与实现
java·开发语言
8Qi814 小时前
LeetCode 121 & 122:股票买卖问题(DP 对比题解)✅
算法·leetcode·职场和发展·动态规划
超梦dasgg14 小时前
Java 生产环境分布式定时任务全解(实战落地版)
java·开发语言·分布式
破土士V14 小时前
Java基础知识集合
java·开发语言
一只齐刘海的猫14 小时前
【Leetcode】 接雨水
java·算法·leetcode
ZC跨境爬虫14 小时前
跟着 MDN 学JavaScript day_5:技能测试——变量实战
java·开发语言·前端·javascript
瑞雪兆丰年兮14 小时前
[0开始学Java|第二十四天]集合(Map&可变参数&集合工具类Collections)
java·开发语言·map·collections
鱼鳞_15 小时前
苍穹外卖-Day12(数据统计)
java·spring boot
phltxy15 小时前
Spring AI Alibaba 多模态应用开发实践
java·人工智能·spring