Leetcode 345. Reverse Vowels of a String

Problem

Given a string s, reverse only all the vowels in the string and return it.

The vowels are ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’, and they can appear in both lower and upper cases, more than once.

Algorithm

Collect all the vowels and reverse the order, then relpace depend on the origional order.

Code

class Solution:
    def reverseVowels(self, s: str) -> str:
        vowels = []
        slen = len(s)
        for i in range(slen):
            if s[i] == 'a' or s[i] == 'e' or s[i] == 'i' or s[i] == 'o' or s[i] == 'u' or s[i] == 'A' or s[i] == 'E' or s[i] == 'I' or s[i] == 'O' or s[i] == 'U':
                vowels.append(s[i])
        vowels.reverse()
        j = 0
        ans = ""
        for i in range(slen):
            if s[i] == 'a' or s[i] == 'e' or s[i] == 'i' or s[i] == 'o' or s[i] == 'u' or s[i] == 'A' or s[i] == 'E' or s[i] == 'I' or s[i] == 'O' or s[i] == 'U':
                ans += vowels[j]
                j += 1
            else: ans += s[i]
        return ans

相关推荐

  1. Leetcode 344. Reverse String

    2023-12-08 23:46:04       57 阅读
  2. Leetcode 345. Reverse Vowels of a String

    2023-12-08 23:46:04       52 阅读
  3. leetcode344. 反转字符串

    2023-12-08 23:46:04       66 阅读
  4. LeetCode344 -反转字符串

    2023-12-08 23:46:04       37 阅读
  5. leetcode 343.整数拆分

    2023-12-08 23:46:04       42 阅读
  6. LeetCode342. 4的幂

    2023-12-08 23:46:04       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-08 23:46:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-08 23:46:04       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-08 23:46:04       87 阅读
  4. Python语言-面向对象

    2023-12-08 23:46:04       96 阅读

热门阅读

  1. 什么是ElasticSearch中的过滤器?

    2023-12-08 23:46:04       65 阅读
  2. golang开发框架

    2023-12-08 23:46:04       55 阅读
  3. SpringBoot学习笔记-实现微服务:匹配系统(下)

    2023-12-08 23:46:04       59 阅读
  4. C语言文本模式和二进制模式

    2023-12-08 23:46:04       59 阅读
  5. Opencv获取笔记本摄像头

    2023-12-08 23:46:04       53 阅读
  6. 跨框架解决方案-Mitosis【Context】

    2023-12-08 23:46:04       65 阅读
  7. vue在哪个生命周期内调用异步请求

    2023-12-08 23:46:04       54 阅读