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

编写一个函数,以字符串作为输入,反转该字符串中的元音字母。

示例 1:

输入: "hello"
输出: "holle"

示例 2:

输入: "leetcode"
输出: "leotcede"

说明:
元音字母不包含字母"y"。



思路

设立2个指针,一个从索引0开始向右,一个从末尾向前,根据条件进行处理即可

代码

class Solution:
    def reverseVowels(self, s):
        """
        :type s: str
        :rtype: str
        """
        yuan = ['a','e','i','o','u','A','E','I','O','U']
        s = list(s)

        l = 0
        r = len(s)-1
        while l < r:
            if s[l] in yuan and s[r] in yuan:
                s[l], s[r] = s[r], s[l]
                l += 1
                r -= 1
            elif s[l] in yuan and s[r] not in yuan:
                r -= 1
            elif s[l] not in yuan and s[r] in yuan:
                l += 1
            else:
                l += 1
                r -= 1
        return ''.join(s)

原文地址:https://www.cnblogs.com/MartinLwx/p/9806368.html

时间: 2024-08-28 02:29:01

Leetcode 345. 反转字符串中的元音字母 By Python的相关文章

LeetCode:反转字符串中的元音字母【345】

LeetCode:反转字符串中的元音字母[345] 题目描述 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 2: 输入: "leetcode" 输出: "leotcede" 说明:元音字母不包含字母"y". 题目分析 所谓的做题就是把以前背下来的拿过来改一下即可.双指针碰撞模型,之前已经描述过很多次了,此处不在赘述. 知道AEI

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

编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 2: 输入: "leetcode" 输出: "leotcede" 说明: 元音字母不包含字母"y" 1/** 2 * @param {string} s 3 * @return {string} 4 */ 5function judgeVowel(c) { 6    return c

[LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母

Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede". 这道题让我们翻转字符串中的元音字母,元音字母有五个a,e,i,o

双指针---反转字符串中的元音字符

反转字符串中的元音字符 345. Reverse Vowels of a String (Easy) Given s = "leetcode", return "leotcede" 题目描述: ??给定一个字符串,将字符串中的元音字母交换,返回交换后的字符串. 思路分析: ??使用双指针指向待反转的两个元音字符,一个指针从头向尾进行遍历,一个指针从尾到头遍历. 代码: private final static HashSet<Character>vowe

字符串中的元音字母翻转输出

字符串中的元音字母翻转 345. Reverse Vowels of a String QuestionEditorial Solution My Submissions Total Accepted: 31038 Total Submissions: 86077 Difficulty: Easy Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv

问题 B: 习题7-7 复制字符串中的元音字母

问题 B: 习题7-7 复制字符串中的元音字母 时间限制: 1 Sec  内存限制: 12 MB献花: 160  解决: 139[献花][花圈][TK题库] 题目描述 写一个函数,将一个字符串中的元音字母复制到另一个字符串中.在主函数中输入一个字符串,通过调用该函数,得到一个有该字符串中的元音字母组成的一个字符串,并输出. 输入 一个字符串(一行字符). 输出 该字符串所有元音字母构成的字符串.行尾换行. 样例输入 CLanguage 样例输出 auae 提示 可以采用如下函数原型 void v

345. 反转字符串中元音字母的位置 Reverse Vowels of a String

Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede" 题意:反转字符串中元音字母的位置 方法1:用栈保存元音字符串,时间

Python Codecademy 练习:去掉字符串中的元音字母

1 def anti_vowel(text): 2 out=[] 3 mystring=list(text) 4 for i in mystring: 5 if i not in ["a","e","i","o","u","A","E","I","O","U"]: 6 out.append(i) 7 prin

Leetcode 557.反转字符串中的单词III

反转字符串中的单词III 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL tsetnoc"  注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格. 实现思路: 刚看题目心想直接用String.split会不会更容易些,不过这样就失去了这个算法的意义了.还是采用最原始的方法,先