1 static int wing=[]() 2 { 3 std::ios::sync_with_stdio(false); 4 cin.tie(NULL); 5 return 0; 6 }(); 7 8 class Solution 9 { 10 public: 11 string reverseVowels(string s) 12 { 13 int len=s.length(); 14 int i=0,j=len-1; 15 while(i<j) 16 { 17 while(!isVowels(s[i])&&i<j) 18 i++; 19 while(!isVowels(s[j])&&i<j) 20 j--; 21 swap(s[i++],s[j--]); 22 } 23 return s; 24 } 25 26 bool isVowels(char c) 27 { 28 c=toupper(c); 29 if((c=='A')||(c=='E')||(c=='I')||(c=='O')||(c=='U')) 30 return true; 31 return false; 32 } 33 };
还是前后指针交换,但是要注意必须使指针指向元音字母。
浙公网安备 33010602011771号