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
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
引言 马上就要举行年会抽奖了,我们都不知道是否有人能够中奖。我觉得无聊的时候可以尝试自己写一个抽奖系统,主要是为了娱乐。现在人工智能这么方便,写一个简单的代码不是一件困难的事情。今天我想和大家一起构建一个简易的抽奖系统,这样也能够巩固一下我自己对Python语…