Leetcode 345 Reverse Vowels in a String
两个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的更多相关文章
- 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 ...
- 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 ...
- Leetcode 345 Reverse Vowels of a String 字符串处理
题意:倒置字符串中的元音字母. 用两个下标分别指向前后两个相对的元音字母,然后交换. 注意:元音字母是aeiouAEIOU. class Solution { public: bool isVowel ...
- LeetCode 345. Reverse Vowels of a String(双指针)
题意:给定一个字符串,反转字符串中的元音字母. 例如: Input: "leetcode" Output: "leotcede" 法一:双指针 class So ...
- 【leetcode】345. Reverse Vowels of a String
problem 345. Reverse Vowels of a String class Solution { public: string reverseVowels(string s) { , ...
- 345. Reverse Vowels of a String - LeetCode
Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...
- 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 ...
- 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 ...
- 【一天一道LeetCode】#345. Reverse Vowels of a String
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
随机推荐
- Sqlite && EF Code FIRST 终极解决方案 2019.5.17
Sqlite && EF Code FIRST 终极解决方案 2019.5.17 包括根据模型自动生成数据库,初始化数据,模型改变时的自动数据迁移等 2019.12.25 更新 支持E ...
- springboot下自定义配置文件,并在项目里读取的方法
首先 pom文件引入springboot文件处理器 <dependency> <groupId>org.springframework.boot</groupId> ...
- py测试一个Socket实例
本实例旨在了解py和socket的一些相关知识. 1.服务器端搭建py监听程序. 在客户端搭建python,linux默认自带了python2.7,先不管安装了. 接着编写socket程序,可以在本地 ...
- 1 A+B问题
原题网址: http://www.lintcode.com/zh-cn/problem/a-b-problem/# 给出两个整数a和b, 求他们的和, 但不能使用 + 等数学运算符. 注意事项 你不需 ...
- POJ-1260-Pearls-dp+理解题意
In Pearlania everybody is fond of pearls. One company, called The Royal Pearl, produces a lot of jew ...
- stop slave->reset slave->start slave 复制从哪个位置开始?reset slave all呢?
reset slave首先来看下当前master-slave情况 mysql> prompt \u@\h,\p:\d>\_ PROMPT set to '\u@\h,\p:\d>\_ ...
- python Six 模块
Six模块用于python2和python3的兼容 import six 官网链接:https://six.readthedocs.io/
- 【LGP5437】【XR-2】约定
题目 显然每一条边出现在生成树中的方案数是相等的 根据矩阵树定理,\(n\)个节点的完全图生成树个数是\(n^{n-2}\)种,完全图共有\(\frac{n(n-1)}{2}\)条边,一棵生成树共\( ...
- HYNB Round 15: PKU Campus 2019
HYNB Round 15: PKU Campus 2019 C. Parade 题意 将平面上n*2个点安排在长度为n的两行上. 做法 首先可以忽略每个点之间的影响,只用考虑匹配即可 然后把所以点归 ...
- 用星星画菱形--Java
用星星画菱形 public class Hello{ public static void main(String[] args) { char star = '\u2605'; System.out ...