1、问题描述

Reverse Vowels of a String

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

Note: The vowels does not include the letter "y".

题目要求是将一个输入string 中的元音字母位置反转,即第一个元音字母和最后一个元音字母交换位置。

,以此类推。

2、题目分析

题目和反转字符串很相似,只不过需要判断一个字符是否为元音字母。思路是两个指针一个从前往后遍历,遇到元音字母停下;另外一个从后往前遍历,遇到元音字母停下。然后交换位置。

3、代码

 string reverseVowels(string s) {

         string vow = "aeiouAEIOU";
int i ,j; for( i = ,j = s.size() - ; i < j ; )
{
if( vow.find( s[i]) != string::npos && vow.find( s[j] ) != string::npos )
swap(s[i],s[j]),i++,j--;
if(vow.find( s[i]) == string::npos )
i++;
if( vow.find(s[j]) == string::npos )
j--;
} return s; }

leetCode题解之反转字符串中的元音字母的更多相关文章

  1. LeetCode:反转字符串中的元音字母【345】

    LeetCode:反转字符串中的元音字母[345] 题目描述 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "h ...

  2. Java实现 LeetCode 345 反转字符串中的元音字母

    345. 反转字符串中的元音字母 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 ...

  3. Leetcode 345. 反转字符串中的元音字母 By Python

    编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 2: 输入: "leet ...

  4. 【leetcode 简单】 第八十三题 反转字符串中的元音字母

    编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 2: 输入: "leet ...

  5. [Swift]LeetCode345. 反转字符串中的元音字母 | Reverse Vowels of a String

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

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

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

  7. Python实现 "反转字符串中的元音字母" 的方法

    #coding=utf- def reverseVowels(s): """ :type s: str :rtype: str """ sS ...

  8. C#LeetCode刷题之#345-反转字符串中的元音字母​​​​​​​(Reverse Vowels of a String)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3935 访问. 编写一个函数,以字符串作为输入,反转该字符串中的元 ...

  9. [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母

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

随机推荐

  1. 48位MAC转化为唯一的128位IPV6地址

    根据EUI_64规范,一个MAC地址生成唯一的一个IPV6地址. ①.反转MAC的第七位为1. ②.在24bit后加入FFFE. ③.在最前面加上FE80::. 示例:

  2. 【jQuery源码】select方法

    /** * select方法是Sizzle选择器包的核心方法之一,其主要完成下列任务: * 1.调用tokenize方法完成对选择器的解析 * 2.对于没有初始集合(即seed没有赋值)且是单一块选择 ...

  3. 【树】Path Sum II(递归)

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

  4. Nginx安装图片模块出错,提示fatal error: curl/curl.h

    获得安装包,从网上直接下载下载地址:https://curl.haxx.se/download.html 然后解压安装后就可以了 # # cd curl- # ./configure # make & ...

  5. Chapter 3 Phenomenon——8

    I turned to sit up, and this time he let me, releasing his hold around my waist and sliding as far f ...

  6. 127.0.0.1和0.0.0.0和本机IP的区别

    在一次网络课程的听课中,我突然察觉到自己有个疑惑就是在配置一些服务的时候我们会用到localhost(127.0.0.1)或者0.0.0.0 和当前主机IP这三个.那么具体该怎么使用这三个地址,这三个 ...

  7. 04-python的列表操作

    python中列表的使用最多, 常用的方法有: append(value) extend(L) 添加指定列表的所有元素 insert(index, value) 插入 remove(value) po ...

  8. Golang cron 定时任务使用

    1.cron 表达式的基本格式 用过 linux 的应该对 cron 有所了解.linux 中可以通过 crontab -e 来配置定时任务.不过,linux 中的 cron 只能精确到分钟.而我们这 ...

  9. Socket编程 - API

    基础知识部分:http://www.cnblogs.com/Jimmy1988/p/7839940.html 1. 基本流程 Process Client Server Comment socket( ...

  10. 解决Tomcat出现内存溢出的问题

    Tomcat服务器出现java.lang.OutOfMemoryError:Java heap space异常 1.可能是程序错误,比如:程序陷入死循环 2.堆内存太小 一般情况下,java创建的对象 ...