Wednesday, July 20, 2016

345. Reverse Vowels of a String

Easy, but easy to make mistake. I've highlighted in red the mistakes I made.

1:  class Solution {  
2:  public:  
3:    string reverseVowels(string s) {  
4:      if (s.empty()) return s;  
5:      int i = 0, j = s.size() - 1;  
6:      while (i < j) {  
7:        while (i < j && !isVowel(s[i])) i++;  
8:        while (i < j && !isVowel(s[j])) j--;  
9:        swap(s[i], s[j]);  
10:        i++, j--;  
11:      }  
12:      return s;  
13:    }  
14:    bool isVowel(char c) {  
15:      c = tolower(c);  
16:      return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';  
17:    }  
18:  };  

No comments:

Post a Comment