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

这道题让我们翻转字符串中的元音字母,元音字母有五个a,e,i,o,u,需要注意的是大写的也算,所以总共有十个字母。我们写一个isVowel的函数来判断当前字符是否为元音字母,如果两边都是元音字母,那么我们交换,如果左边的不是,向右移动一位,如果右边的不是,则向左移动一位,参见代码如下:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;string reverstring(string& );
bool isvowels(char);
void swap1(string&, int, int);
int main()
{string a[] = { "hello", "leetcode", "asjaojhe", "opaioia" };for (string data : a)cout << data << ' ' << reverstring(data) << endl;system("pause");return 0;
}
string reverstring(string& a)
{int n = a.size() - 1;int m = 0;//while (m < n)//{//   if (isvowels(a[m]) && isvowels(a[n]))// {//     /*swap1(a,m,n);*///     swap(a[m], a[n]);//     m++;//        n--;//  }// else//  {//     if (isvowels(a[m]))//       {//         n--;//      }//     else//      {//         m++;//        }// }//}while (m < n){m = a.find_first_of("aeiouAEIOU", m);n = a.find_last_of("aeiouAEIOU", n);if (m < n){swap(a[m], a[n]);m++;n--;}}return a;}bool isvowels(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;elsereturn false;
}
void swap1(string& a,int m, int n)
{char temp = a[m];a[m] = a[n];a[n] = temp;
}
class Solution {
public:string reverseVowels(string s) {int m=0;int n=s.size()-1;while(m<n){if(isVowel(s[m])&& isVowel(s[n])){swap(s[m],s[n]);m++;n--;}else{if(isVowel(s[m]))n--;else{m++;}}}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;  else  return false;  }
};

Reverse Vowels of a String相关推荐

  1. Reverse Vowels of a String (反转字符串中的母音)

    leetcode Reverse Vowels of a String 反转字符串中的母音 一.学习要点: 1.find_first_of:查找与字符串str中某个字符相同的位置,并返回他的第一个出现 ...

  2. 345. Reverse Vowels of a String - LeetCode

    Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...

  3. LeetCode345. Reverse Vowels of a String

    345. Reverse Vowels of a String My Submissions QuestionEditorial Solution Total Accepted: 3821 Total ...

  4. 345.反转字符串中的元音字母(Reverse Vowels of a String)

    题目描述 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 给定 s = "hello", 返回 "holle". 示例 2: 给定 s ...

  5. 从零开始的LC刷题(74): Reverse Vowels of a String

    原题: Write a function that takes a string as input and reverse only the vowels of a string. Example 1 ...

  6. LeetCode:345. Reverse Vowels of a String

    051103 题目 Write a function that takes a string as input and reverse only the vowels of a string. Exa ...

  7. leetcode345——Reverse Vowels of a String(C++)

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Gi ...

  8. LeetCode Reverse Vowels of a String

    原题链接在这里:https://leetcode.com/problems/reverse-vowels-of-a-string/ 题目: Write a function that takes a ...

  9. LeetCode 345. Reverse Vowels of a String

    题目: Write a function that takes a string as input and reverse only the vowels of a string. Example 1 ...

最新文章

  1. 《人工智能转型手册》,吴恩达 著
  2. 内存地址 哪个程序_记一次排查线上程序内存的忽高忽低,又是大集合惹祸了...
  3. Shell中的常用操作
  4. 20 条非常实用的 Python 代码,建议收藏!
  5. norflsh nandflash之类的存储设备启动bootloader概述
  6. 一时冲动:“通往瓦尔哈拉之路的冒险”
  7. 一篇英文文档中找出频数最多的10个单词
  8. json web token没有哪个成分_SpringBoot 2.1.4集成JWT实现token验证
  9. CMake with WinMinGW
  10. 深入浅出设计模式——解释器模式(Interpreter Pattern)
  11. HDoj-1863-畅通project-并查集
  12. 宁德时代预计一季度净利润超9.9亿元 同比增长超140%
  13. 丁胖胖眼中的WINDOWS操作系统(一)
  14. 调试系列1:bugreport源码篇
  15. list.stream().map().collect(Collectors.toList())
  16. AutoCAD VBA二次开发地形图多边形裁剪
  17. The kernel appears to have died. It will restart automatically.
  18. iOS15适配本地通知功能
  19. bcdedit删除引导_Bcdedit命令教程以及用于编辑Windows引导配置的示例
  20. Ubuntu清理系统垃圾 命令

热门文章

  1. mx:button加skin光晕点击时,大小不一样
  2. SQL Server-创建表格、各种约束条件
  3. git diff命令输出的含义
  4. 使用jQuery.Ajax向ASP.NET MVC控制器Post数据
  5. ElasticSearch _xpack用户管理
  6. 亚信安全中标民生银行防病毒项目 为“互联网+金融”革新防护能力
  7. 电脑达人教你怎么解决Win7系统盘渐渐变小的问题
  8. Events are a bad idea?
  9. Alex学Ruby[详解 block和Proc对象]
  10. 概率编程库Pymc3案例之神经网络