LeetCode 345

这是一个关于字符串处理的算法问题,要求编写一个函数,接收一个字符串输入,然后反转该字符串中的元音字母。元音不包括'y'。示例中给出了'hello'变为'hollE','leetcode'变为'leotcede'的转换。解决方案使用了双指针技术,从字符串两端同时向中间遍历,找到元音并交换。
摘要由CSDN通过智能技术生成
//编写一个函数,以字符串作为输入,反转该字符串中的元音字母。 
//
// 
//
// 示例 1: 
//
// 输入:"hello"
//输出:"holle"
// 
//
// 示例 2: 
//
// 输入:"leetcode"
//输出:"leotcede" 
//
// 
//
// 提示: 
//
// 
// 元音字母不包含字母 "y" 。 
// 
// Related Topics 双指针 字符串 
// 👍 188 👎 0
//a、e、i、o、u

//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public String reverseVowels(String s) {
        int len=s.length();
        int left=0;
        int right=len-1;
        char[] chars=s.toCharArray();
        while(left<right){
            while(left<len&&!contain(chars[left])){
                left++;
            }
            while(right>=0&&!contain(chars[right])){//为啥不是>=0
                right--;
            }
            if(left<right){
                char tmp=chars[left];
                chars[left]=chars[right];
                chars[right]=tmp;
                left++;
                right--;
            }
        }
        return new String(chars);
    }
    public boolean contain(char c){
        if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U'){
            return true;
        }
        return false;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值