[LeetCode][JavaScript][Python]Reverse Vowels of a String

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, u,包含大小写。

双指针,一个在头一个在尾,找了元音就交换两个指针指向的内容。

最近在学python,之后会尽量用python练习。

JS:

 1 /**
 2  * @param {string} s
 3  * @return {string}
 4  */
 5 var reverseVowels = function(s) {
 6     var i, res = s.split(‘‘), start = 0, end = s.length - 1,
 7     aVowels = [‘a‘, ‘e‘, ‘i‘, ‘o‘, ‘u‘];
 8     while(start < end){
 9         while(start < end && !isVowel(s[start]))
10             start++;
11         while(start < end && !isVowel(s[end]))
12             end--;
13         if(start !== end)
14             swap(start, end);
15         start++;
16         end--;
17     }
18     return res.join(‘‘);
19
20     function isVowel(char){
21         if(aVowels.indexOf(char.toLowerCase()) !== -1){
22             return true;
23         }
24         return false;
25     }
26     function swap(i, j){
27         var temp = res[i];
28         res[i] = res[j];
29         res[j] = temp;
30     }
31 };

Python:

 1 class Solution(object):
 2     def reverseVowels(self, s):
 3         res = list(s)
 4         vowels = [‘a‘, ‘e‘, ‘i‘, ‘o‘, ‘u‘]
 5         start = 0; end = len(s) - 1
 6         while start < end:
 7             while start < end and (s[start].lower() not in vowels):
 8                 start += 1
 9             while start < end and (s[end].lower() not in vowels):
10                 end -= 1
11             if(start != end):
12                 tmp = res[start]
13                 res[start] = res[end]
14                 res[end] = tmp
15             start += 1; end -= 1
16         return ‘‘.join(res)
时间: 2024-11-12 10:41:30

[LeetCode][JavaScript][Python]Reverse Vowels of a String的相关文章

【LeetCode】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". 应该算不上有难度. class Solution { p

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". 题目解答: 要求将字符串中所有的元音字母逆转,辅音字

&lt;LeetCode OJ&gt; 345. Reverse Vowels of a String

Total Accepted: 537 Total Submissions: 1488 Difficulty: Easy 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"

345. Reverse Vowels of a String(C++)

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"

345. Reverse Vowels of a String【easy】

345. Reverse Vowels of a String[easy] 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&q

LeetCode_345. Reverse Vowels of a String

345. Reverse Vowels of a String Easy Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Input: "hello" Output: "holle" Example 2: Input: "leetcode" Output: "leotcede" N

Leetcode 151题 Reverse Words in a String

时间:2014.05.10 地点:基地 心情:准备刷下Leetcode题了,就从第151题开始吧 ------------------------------------------------------------------------------------------ 一.题目 Reverse Words in a String Given an input string, reverse the string word by word. For example, Given s =

【leetcode】557. Reverse Words in a String III

Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-in-a-string-iii/ 1)problem Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace

LeetCode 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". Note:The vowels does not include