Leetcode345 -- Reverse Vowels of a String (C ++), leetcode345vowels
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 ".
Blog: http://www.cnblogs.com/wdfwolf3 /.
This topic adds a limit to the reverse string, and only performs reverse settings for the original audio (aeiouAEIOU), ignoring non-cause characters. Here I will mainly discuss several methods for determining the cause. As for String inversion, I will not discuss it any more. For details, refer to my blog post dedicated to Reverse String: http://www.cnblogs.com/wdfwolf3/p/5484675.html.
1. Call function judgment (16 ms)
This is the most basic and easy to think.
Class Solution {public: string reverseVowels (string s) {int I = 0, j = s. length ()-1; while (I <j) {while (isVowel (s [I]) = false) & (I <j )) {I ++;} while (isVowel (s [j]) = false) & (I <j) {j --;} swap (s [I], s [j]); I ++; j --;} return s;} bool isVowel (char c) {if (c = 'A ') | (c = 'E') | (c = 'I') | (c = 'O') | (c = 'U ') | (c = 'A') | (c = 'E') | (c = 'I') | (c = 'O ') | (c = 'U') return true; return false ;}};View Code
2. Use arrays to simulate hash tables (12 ms)
Class Solution {public: string reverseVowels (string s) {int dict [128] = {0}; dict ['a'] = 1; dict ['a'] = 1; dict ['E'] = 1; dict ['E'] = 1; dict ['I'] = 1; dict ['I'] = 1 dict ['O'] = 1; dict ['O'] = 1; dict ['U'] = 1; dict ['U'] = 1; int I = 0, j = s. length ()-1; while (I <j) {while (dict [s [I] = 0) & (I <j )) {I ++;} while (dict [s [j] = 0) & (I <j) {j --;} swap (s [I], s [j]); I ++; j --;} return s ;}};View Code
3. Use the string search function string. find () or string. find_first_of (). These two functions are described in detail at the end.
Class Solution {public: string reverseVowels (string s) {string vowel = "aeiouAEIOU"; int I = 0, j = s. length ()-1; while (I <j) {while (vowel. find (s [I]) = string: npos) & (I <j) {I ++;} while (vowel. find (s [j]) = string: npos) & (I <j) {j --;} swap (s [I], s [j]); I ++; j --;} return s ;}};View Code (12 ms) string vowel = "aeiouAEIOU"; int I = 0, j = s. length ()-1; while (I <j) {I = s. find_first_of (vowel, I); j = s. find_last_of (vowel, j); if (I> = j) break; swap (s [I], s [j]); I ++; j --;} return s;View Code (13 ms)
P.S.
1. str1.find (str2 ,,)
The function is to find the position of str2 in str1. str2 can be a single character, string variable, or string. The second parameter is the start position of str1, that is, the start position of str1, the default value is 0. The third parameter only searches for the first number of characters in str2. The default value is all characters in str2. The last two parameters are not allowed. Returns the first character of str2 at the position of str1. If it is not found, it returns string: npos, which has several meanings. It is a constant-1, when the return value is used, the search fails. When the submark is used, it is greater than any subscript (logically defined) and can be seen as the end or end of a string.
2. string. find_first_of (str ,,)
Parameters are used in the same way. However, the function functions are different. The returned value is the position where any character in str appears in string for the first time. The previous function is equivalent to matching, which is more like filtering.
3. string. find_last_of (str ,,)
The same is true for parameters. The difference is that this is the forward lookup. Starting from the second parameter position, find the position where any character in str appears in string for the first time. The default parameter is string: npos.