当前位置: 首页 > news >正文

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 = 0ans = ""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 += 1else: ans += s[i]return ans
http://www.lryc.cn/news/256779.html

相关文章:

  • [linux] 用命令行wget下载google drive的大文件
  • Docker Network(网络)——8
  • 网页设计--第6次课后作业
  • R语言学习
  • 基于Unity3D 低多边形地形模型纹理贴图
  • vue预览pdf,放大缩小拖动,dialog拖动,父页面滚动
  • 泽攸科技二维材料转移台的应用场景及优势
  • JavaScript——基本使用HelloWrold
  • 关于DNS服务器地址总是127.0.0.1且无法解析域名地址
  • 制作一个RISC-V的操作系统四-嵌入式开发介绍
  • Python爬虫-实现批量抓取王者荣耀皮肤图片并保存到本地
  • 04-详解Eureka注册中心的作用,具体配置,服务注册和服务发现
  • 数据分析基础之《matplotlib(3)—散点图》
  • Flask 动态路由、请求数据接收、视图函数返回值详解
  • Transformer 简介
  • LLVM学习笔记(64)
  • 深度学习TensorFlow2基础知识学习前半部分
  • Linux系统---简易伙伴系统
  • Redis使用Lua脚本
  • macos安装metal 加速版 pytorch
  • 【学习笔记】lyndon分解
  • 21、命令执行
  • Qexo博客后台管理部署
  • 最小生成树prim
  • 实用篇 | 一文学会人工智能中API的Flask编写(内含模板)
  • Si24R03—低功耗 SOC 芯片(集成RISC-V内核+2.4GHz无线收发器)
  • C# Winform 日志系统
  • 【Java 基础】27 XML 解析
  • 地图服务 ArcGIS API for JavaScript基础用法全解析
  • docker学习(八、mysql8.2主从复制遇到的问题)