Leetcode345--reverse vowels of a String (C + +)

Source: Internet
Author: User

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".

Personal blog: http://www.cnblogs.com/wdfwolf3/.

This problem adds a limit to the inverse of the string, only the inverse of the original (Aeiouaeiou), ignoring non-causal characters. Here I mainly for how to determine the reasons to tell a few methods, as to the string inverse is no longer discussed, you can see my special writing reverse string http://www.cnblogs.com/wdfwolf3/p/5484675.html.

1. Call function Judgment (16MS)

This is the most basic and easy to think of implementation, no difficulty.

classSolution { Public:    stringReversevowels (strings) {intI=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--; }        returns; }    BOOLIsvowel (Charc) {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. How to simulate a hash table using an array (12ms)

classSolution { Public:    stringReversevowels (strings) {intdict[ -] = {0}; dict['a']=1; dict['A']=1; dict['e']=1; dict['E']=1; dict['I']=1; dict['I']=1dict['o']=1; dict['O']=1; dict['u']=1; dict['U']=1; intI=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--; }        returns; }};
View Code

3. Use the string to find the function string.find () or string.find_first_of (). About these two functions are described in detail in the last side.

classSolution { Public:    stringReversevowels (strings) {stringVowel="Aeiouaeiou"; intI=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--; }        returns; }};
View Code (12ms)
stringVowel="Aeiouaeiou"; intI=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--; }        returnS
View Code (13MS)

P.S.

1.str1.find (STR2,,)

The function is to find the position of str2 in str1, str2 can be a single character, a string variable or a string, the second argument is the starting position of the str1, that is, where to start from STR1, the default is 0, and the third parameter is to find only the first number of characters in str2, The default is all of str2. There can be no latter two arguments. Returns the position of the first character of str2 in str1, if not found, returns STRING::NPOS, which has several layers of meaning, itself it is a constant-1, when the return value indicates that the lookup failed, when using subscript, it is greater than any subscript (the logical concept), Can be thought of as the end of a string or end.

2.string.find_first_of (str,,)

Parameter function as above. However, the function is different, and the returned value is the position of any one of the characters in Str that first appears in the string. The previous function corresponds to a match, which is more like a filter.

3.string.find_last_of (str,,)

The function of the parameter is ibid. The difference is that this is a forward lookup, starting from the second parameter position to find the first occurrence of any character in Str in a string where the natural default parameter is String::npos.

Leetcode345--reverse vowels of a String (C + +)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.