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 ...
随机推荐
- Ros node启动与关闭
1. ros运行单位: Ros程序运行的单位是ros node. 2. ros 节点的启动: (1)初始化ros节点:通过调用ros::init()接口实现:可以通过参数指定默认节点名字,之所以是默认 ...
- django中写form表单时csrf_token的作用
之前在学习django的时候,在template中写form时,出现错误.百度,google后要加{% csrf_token %}才可以,之前一直也没研究,只是知道要加个这个东西,具体是什么也不明白. ...
- 多线程--GIL锁
GIL 即全局解释器锁,是一个互斥锁,防止多个线程在同一时间执行python代码,因为在一个python进程中,不仅有主线程而且还有该主线程开启的子线程,还有解释器开启的垃圾回收机等解释器级别的线程. ...
- 多进程并发socket通信
实现多个客户端同时接入server端,并且可以同时向客户端发送信息 server端 def dunc(conn,client_addr): while True: data=conn.recv(102 ...
- pycharm同时使用python2.7和python3.5设置方法
pycharm同时使用python2.7和python3.5设置方法 - CSDN博客https://blog.csdn.net/qwerty200696/article/details/530159 ...
- 通过aapt查看apk包名和第一个启动的activity
步骤: ps:aapt是sdk 自带的一个工具,在sdk\builds-tools目录下: 1. cmd启动控制台, 默认是c盘,输入“d:” 即可转到D盘目录 2. 到D盘后 输入cd 子文件目录转 ...
- java8 新特性学习笔记
Java8新特性 学习笔记 1主要内容 Lambda 表达式 函数式接口 方法引用与构造器引用 Stream API 接口中的默认方法与静态方法 新时间日期 API 其他新特性 2 简洁 速度更快 修 ...
- 逆元 组合A(n,m) C(n,m)递推 隔板法
求逆元 https://blog.csdn.net/baidu_35643793/article/details/75268911 int inv[N]; void init(){ inv[] = ; ...
- Walk Through Squares HDU - 4758 AC自动机+简单状压DP
题意:给你两个串,求用m个R,n个D能组成多少个包含这两个串 题解:先构造一个AC自动机记录每个状态包含两个串的状态, 状态很容易定义 dp[i][j][k][status]表示在AC自动机K这个节点 ...
- JSONObjectSample
package com.egeniuss.platform.basic; import java.util.ArrayList; import java.util.HashMap; import ja ...