天增的博客
首页
博客
  • 分布式解决方案
  • Java并发工具包
  • redis
  • LeetCode
  • 系统设计
  • JVM体系
Github (opens new window)
Rss (opens new window)
  • zh-CN
  • en-US
首页
博客
  • 分布式解决方案
  • Java并发工具包
  • redis
  • LeetCode
  • 系统设计
  • JVM体系
Github (opens new window)
Rss (opens new window)
  • zh-CN
  • en-US
  • LeetCode
  • 双指针
    • 26.删除有序数组中的重复项
    • 80.删除有序数组中的重复项2
    • 27.移除元素
    • 167.两数之和 II - 输入有序数组
    • 283.移动零
    • 125.验证回文串
    • 344.反转字符串
    • 11.盛最多水的容器
    • 345.反转字符串中的元音字母
  • topic
  • LeetCode
  • 双指针
  • 反转字符串中的元音字母
2022-05-30

345.反转字符串中的元音字母

# 345.反转字符串中的元音字母

LeetCode链接: https://leetcode.cn/problems/reverse-string/ (opens new window)

题目描述: 反转字符串中的元音字母。元音字母包括 'a'、'e'、'i'、'o'、'u',且可能以大小写两种形式出现。

输入:s = "hello"
输出:"holle"

这道题的题型和125.验证回文串类似,区别就是一个是反转另一个是比较。

  • 声明左右指针
  • 将左右指针移动到有效的符号位置
  • 开始交换元素
public String reverseVowels(String s) {
    char[] chars = s.toCharArray();
    int left = 0;
    int right = chars.length - 1;

    while (left < right){
        while (left < right && !isV(chars[left])){
            left++;
        }
        while(left < right && !isV(chars[right])){
            right--;
        }
        char t = chars[left];
        chars[left] = chars[right];
        chars[right] = t;
        left++;
        right--;
    }
    return new String(chars);
}
public boolean isV(char ch) {
    return "aeiouAEIOU".indexOf(ch) >= 0;
}
最近更新
01
以 root 身份启动 transmission-daemon
12-13
02
Debian系统安装qbittorrent-nox
12-09
03
LXC Debain12安装zerotier并实现局域网自动nat转发
07-29
更多文章>
Theme by Vdoing | Copyright © 2015-2024 天增 | 苏ICP备16037388号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式