两个for

第一个for将每一个元音依次存放进一个char数组

第二个for,每检测到元音,就从char数尾部开始,依次赋值

如何检测元音呢?当然写一个冗长的if(),不过我们有更好的选择

hashset的contains,

或者String自带的contains,

或者建一个int[128],因为元音有5个,算上大小写,一共10个,他们的ascii值都在128以内,然后将元音对应的int[]值设为1,其它设为0,只要检测int[strs[i]] ?= 0即可判断是否为元音

 class Solution {
public String reverseVowels(String s) {
if(s.length() == 0)
return ""; String v = "aeiouAEIOU";
char[] vowel = new char[s.length()];
char[] s2 = s.toCharArray();
int idx = 0; for(int i=0; i<s.length(); i++){
if(v.contains(s.charAt(i)+""))
vowel[idx++] = s.charAt(i);
} for(int i=0; i<s.length(); i++){
if(v.contains(s.charAt(i)+""))
s2[i] = vowel[--idx]; }
return new String(s2);
}
}

Leetcode 345 Reverse Vowels in a String的更多相关文章

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

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

  3. Leetcode 345 Reverse Vowels of a String 字符串处理

    题意:倒置字符串中的元音字母. 用两个下标分别指向前后两个相对的元音字母,然后交换. 注意:元音字母是aeiouAEIOU. class Solution { public: bool isVowel ...

  4. LeetCode 345. Reverse Vowels of a String(双指针)

    题意:给定一个字符串,反转字符串中的元音字母. 例如: Input: "leetcode" Output: "leotcede" 法一:双指针 class So ...

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

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

  6. 345. Reverse Vowels of a String - LeetCode

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

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

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

  9. 【一天一道LeetCode】#345. Reverse Vowels of a String

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...

随机推荐

  1. 一张图轻松掌握 Flink on YARN 应用启动全流程(上)

    Flink 支持 Standalone 独立部署和 YARN.Kubernetes.Mesos 等集群部署模式,其中 YARN 集群部署模式在国内的应用越来越广泛.Flink 社区将推出 Flink ...

  2. duilib教程之duilib入门简明教程13.复杂控件介绍

    首先将本节要介绍的控件全部拖到界面上,并调整好位置,如图:  然后将Name属性改成其他名字,         不能是[控件名+UI+数字]这种,因为这是DuiDesigner默认的名字,它不会实际写 ...

  3. golang中使用gorm连接mysql操作

    一.代码 package main import ( "fmt" "github.com/jinzhu/gorm" _ "github.com/go- ...

  4. spss modeler出现使用错误提

    spss modeler出现使用错误提 1.对字段"compensation汇总导出"指定的类型不充分 问题: 为了分析需要,我加了一个"字段选项"--&quo ...

  5. Spring+SpringMVC+Mybatis搭建的网站的处理流程总结

    最近学习了如何使用SSM框架搭建网站,以前没用过框架,第一次使用,总结一下自己对框架处理流程的理解

  6. springboot跨域访问

    写一个WebMvc配置类重写addCorsMappings即可 @Configuration public class MyMvcConfig implements WebMvcConfigurer ...

  7. 如何在neo4j中创建新数据库?

    解决方案一: 由于使用Neo3.x创建新数据库而不删除现有数据库,所以只需在$NEO4J_HOME的conf的目录编辑neo4j.conf. 搜寻dbms.active_database=,其默认值应 ...

  8. Arrays.asList()使用的问题

    在java语言中,把数组转换成List集合,有个很方便的方法就是 List<String> list = Arrays.asList("a","b" ...

  9. mysql出现ERROR 1366 (HY000):的解决办法

    今天向新建的表中添加内容,出现以下错误: mysql> INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_sho ...

  10. 【BZOJ4916】神犇与蒟蒻

    题面 Description 很久很久以前,有一只神犇叫yzy; 很久很久之后,有一只蒟蒻叫lty; Input 请你读入一个整数N;\(1<=N<=10^9\),A.B模\(10^9+7 ...