您的位置:首页 > 编程语言 > Python开发

【leetcode】(Python)Reverse Vowels of a String实现字符串内元字符倒序

2016-04-24 13:38 731 查看
leetcode:(Python)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".

其实是leetcode上面的一道题目

没有python版本的故来抛砖,经提交可通过

class Solution(object):

    def reverseVowels(self, s):

            string=list(s)

            j=0

            n=0

            m=[]

            k=[]

            for i in s:

                if i=='a'or i=='e' or i=='i' or i=='o' or i=='u' or i=='A'or i=='E' or i=='I' or i=='O' or i=='U':

                    m.append(j)

                j=j+1

            if len(m)>1:

                for i in m:

                    k.append(s[i])

                k.reverse()

                for i in m:

                    string[i]=k

                    n=n+1

            string=''.join(string)

            return string

           
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: