题目描述:

写一个函数,实现输入一个字符串,然后把其中的元音字母倒叙

注意

元音字母包含大小写,元音字母有五个a,e,i,o,u

原文描述:

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

思路:

  • 使用HashSet的数据结构存储者10个字符
  • 遍历字符串,使用int【s.length】记录元音的位置,元音的个数为n
  • 遍历字符串,根据上一个数组,把所有的元音字母(索引为i)换成string.charAt(n-i-1)处的字符

代码:

public class Solution {
    public String reverseVowels(String s) {
        if(s == null){
            return null;
        }
         int[] array = new int[s.length()];
        int index = 0;
        HashSet<Character> vowel = new HashSet<Character>();
        vowel.add('a');
        vowel.add('e');
        vowel.add('i');
        vowel.add('o');
        vowel.add('u');
        vowel.add('A');
        vowel.add('E');
        vowel.add('I');
        vowel.add('O');
        vowel.add('U');

        for (int i = 0; i < s.length(); i++) {
            if (vowel.contains(s.charAt(i))) {
                array[index] = i;
                index++;
            }
        }

        char[] result = new char[s.length()];
        result = s.toCharArray();
        for (int i = 0; i < index; i++) {
            result[array[i]] = s.charAt(array[index - i - 1]);
        }
        return String.valueOf(result);
    }
}

更多的leetcode的经典算法,查看我的leetcode专栏,链接如下:

leetcode专栏

我的微信二维码如下,欢迎交流讨论

欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧,都是干货!

微信订阅号二维码如下:

【leetcode80】Reverse Vowels of a String(元音字母倒叙)的更多相关文章

  1. 345. Reverse Vowels of a String(C++)

    345. Reverse Vowels of a String Write a function that takes a string as input and reverse only the v ...

  2. 【leetcode】345. Reverse Vowels of a String

    problem 345. Reverse Vowels of a String class Solution { public: string reverseVowels(string s) { , ...

  3. 345. Reverse Vowels of a String - LeetCode

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

  4. 345. Reverse Vowels of a String【easy】

    345. Reverse Vowels of a String[easy] Write a function that takes a string as input and reverse only ...

  5. LeetCode_345. Reverse Vowels of a String

    345. Reverse Vowels of a String Easy Write a function that takes a string as input and reverse only ...

  6. [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 ...

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

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

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

  9. 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. c++ 隐藏

    子类中写了一个与从父类中继承下来的方法名同名的方法 子类对象.方法();                    //调用子类中定义的方法 父类对象.方法();                    / ...

  2. Openlayers3学习心得(初识)

    最近刚辞了原来的那家公司,准备新找一份工作.其中有个公司要求会Openlayers3.一看到这个要求,就知道公司业务涉及地图图表比较多. Openlayers本身是一个基于GIS地图相关的功能丰富的J ...

  3. JavaScript for 循环

    循环可以将代码块执行指定的次数. JavaScript 循环 如果您希望一遍又一遍地运行相同的代码,并且每次的值都不同,那么使用循环是很方便的. 我们可以这样输出数组的值: 一般写法: documen ...

  4. Web Worker Best Practices

    使用Web Worker可以把一些比较计算量相对大的阻塞浏览器响应的计算放在单独的线程里计算. 请求优化 构造Worker的时候需要给定js的链接URL,worker内部请求js运行代码.假如work ...

  5. Spark Streaming中的操作函数分析

    根据Spark官方文档中的描述,在Spark Streaming应用中,一个DStream对象可以调用多种操作,主要分为以下几类 Transformations Window Operations J ...

  6. Sqoop-1.4.6 Merge源码分析与改造使其支持多个merge-key

    Sqoop中提供了一个用于合并数据集的工具sqoop-merge.官方文档中的描述可以参考我的另一篇博客Sqoop-1.4.5用户手册. Merge的基本原理是,需要指定新数据集和老数据集的路径,根据 ...

  7. gradle 入门介绍

    gradle 简介 基于Groovy实现的自动化构建工具,比maven好的一点在于不用写复杂的xml文件.使用script就可以. gradle 专业名词 从一个build.gradle 文件开始,b ...

  8. Mac上如何完美的转换epub至mobi供kindle观看

    网上有很多书籍资源的格式都是epub(我们不谈及pdf格式了,你懂得-),epub格式是无法直接在kindle上观赏的,除非你越狱kinde后,安装扩展插件 我们可以将epub转换为mobi格式,网上 ...

  9. 通过grub-install命令把grub安装到u盘

    通过grub-install命令把grub安装到u盘 ①准备一个u盘,容量不限,能有1MB都足够了. ②把u盘格式化(我把u盘格式化成FAT.fat32格式了,最后证明也是成功的).③开启linux系 ...

  10. [ExtJS5学习笔记]第三十二节 sencha extjs 5与struts2的ajax交互配置

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/43487751 本文作者:sushengmiyan ------------------ ...