【双指针+字符串】字符串变形


求解代码

java 复制代码
public String trans(String s, int n) {
        if (s == null || s.length() == 0) {
            return s;
        }
        char[] str = s.toCharArray();
        reverse(str, 0, str.length - 1);
        int i = 0, j = 0;
        while (i < str.length) {
            j = i;
            while (j < str.length && str[j] != ' ') {
                if (str[j] >= 'a' && str[j] <= 'z') {
                    str[j] = (char) (str[j] - 'a' + 'A');
                } else {
                    str[j] = (char) (str[j] - 'A' + 'a');
                }
                j++;
            }
            reverse(str, i, j - 1);
            i = j + 1;
        }
        return new String(str);
    }

    public void reverse(char[] arr, int start, int end) {
        int i = start;
        int j = end;
        while (i < j) {
            char c = arr[i];
            arr[i] = arr[j];
            arr[j] = c;
            i++;
            j--;
        }
    }

小贴士

代码思路大概是:

整体反转➡️单词遍历➡️大小写互换➕局部反转

相关推荐
提子拌饭1332 分钟前
开源鸿蒙跨平台Flutter开发:AR厨艺教学应用
android·flutter·华为·开源·ar·harmonyos·鸿蒙
cheems95276 分钟前
[SpringMVC] Spring MVC 留言板开发实战
java·spring·mvc
BioRunYiXue7 分钟前
AlphaGenome:DeepMind 新作,基因组学迎来 Alpha 时刻
java·linux·运维·网络·数据库·人工智能·eclipse
fengci.10 分钟前
php反序列化(复习)(第四章)
android·开发语言·学习·php·android studio
XiaoLeisj10 分钟前
Android 短视频项目首页开发实战:从广场页广告轮播与网格列表,到发现页分类、播单与话题广场的数据驱动实现
android·okhttp·mvvm·recyclerview·retrofit·databinding·xbanner 轮播
whatever who cares13 分钟前
android中,全局管理数据/固定数据要不要放一起?
android·java·开发语言
C1829818257518 分钟前
AI idea 集成claude code插件
java·ide·intellij-idea
IT 行者19 分钟前
解决 IntelliJ IDEA 内存占用高的两个优化策略:GPU 渲染与虚拟内存配置
java·ide·intellij-idea·ai编程
Aric_Jones21 分钟前
从实战理解异步、并发并行与GIL:FastAPI vs SpringBoot
java·spring boot·fastapi
云烟成雨TD25 分钟前
Spring AI 1.x 系列【27】Chat Memory API:让 LLM 拥有上下文记忆能力
java·人工智能·spring