leetcode 345逆転文字
12341 ワード
leetcodeコード:
c++フルコード:
class Solution {
private:
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;
else
return false;
}
public:
string reverseVowels(string s) {
int i = 0, j = s.size()-1;
while(i < j){/*i j */
if(!isvowel(s[i])){/* */
i++;/* */
continue;/* , , */
}
if(!isvowel(s[j])){/* j */
j--;/* j */
continue;/* */
}
swap(s[i++],s[j--]);/*i j , */
}
return s;/* */
}
};
c++フルコード:
#include
#include
using namespace std;
string reverseVowels(string 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;
else
return false;
}
string reverseVowels(string s) {
int i = 0, j = s.size() - 1;
while (i < j) {/*i j */
if (!isvowel(s[i])) {/* */
i++;/* */
continue;/* , , */
}
if (!isvowel(s[j])) {/* j */
j--;/* j */
continue;/* */
}
swap(s[i++], s[j--]);/*i j , */
}
return s;/* */
}
int main()
{
char str[50];/* : 5 , 5 6 , '\0'*/
cout << "please input a string of characters:";
cin >> str;
cout << reverseVowels(str) << endl;
return 0;
}