C#LeetCode刷题之#345-反转字符串中的元音字母(Reverse Vowels of a String)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3935 访问。
编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
输入: "hello"
输出: "holle"
输入: "leetcode"
输出: "leotcede"
说明:元音字母不包含字母"y"。
Write a function that takes a string as input and reverse only the vowels of a string.
Given s = "hello", return "holle".
Given s = "leetcode", return "leotcede".
Note:The vowels does not include the letter "y".
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3935 访问。
public class Program {
public static void Main(string[] args) {
var s = "holle";
var res = ReverseVowels(s);
Console.WriteLine(res);
s = "leotcede";
res = ReverseVowels2(s);
Console.WriteLine(res);
Console.ReadKey();
}
private static string ReverseVowels(string s) {
//暴力解法
//LeetCode超时未AC
//记录元音字母
var vowels = new List<char>() {
'a','e','i','o','u',
'A','E','I','O','U'
};
//字典记录所有元音字母及位置
var dic = new Dictionary<int, char>();
for(var i = 0; i < s.Length; i++) {
if(vowels.Contains(s[i])) {
dic[i] = s[i];
}
}
//两两前后交换所有元音值
for(var i = 0; i < dic.Count / 2; i++) {
var key1 = dic.ElementAt(i).Key;
var key2 = dic.ElementAt(dic.Count - i - 1).Key;
var swap = dic[key1];
dic[key1] = dic[key2];
dic[key2] = swap;
}
//重新规划元音字符串顺序
var res = new StringBuilder(s);
foreach(var item in dic) {
res[item.Key] = item.Value;
}
//返回结果
return res.ToString();
}
private static string ReverseVowels2(string s) {
//双指针法
//记录元音字母
var vowels = new List<char>() {
'a','e','i','o','u',
'A','E','I','O','U'
};
//转换成 char 数组
//因为 C# 的字符串索引器是只读的,所以这是必须的
var chars = s.ToCharArray();
//前后双指针法
var i = 0;
var j = s.Length - 1;
//循环直到指针碰撞时为止
while(i < j) {
//从左向右找到元音字符
while(i < j && !vowels.Contains(chars[i])) i++;
//从右向左找到元音字符
while(i < j && !vowels.Contains(chars[j])) j--;
//交换它们,注意这里的条件是必须的
if(i < j) {
var swap = chars[i];
chars[i++] = chars[j];
chars[j--] = swap;
}
}
//返回结果
return new string(chars);
}
}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3935 访问。
hello
leetcode
分析:
显而易见,以上2种算法的时间复杂度均为: 。
C#LeetCode刷题之#345-反转字符串中的元音字母(Reverse Vowels of a String)的更多相关文章
- [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 ...
- Java实现 LeetCode 345 反转字符串中的元音字母
345. 反转字符串中的元音字母 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 ...
- Leetcode 345. 反转字符串中的元音字母 By Python
编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 2: 输入: "leet ...
- LeetCode:反转字符串中的元音字母【345】
LeetCode:反转字符串中的元音字母[345] 题目描述 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "h ...
- 【leetcode 简单】 第八十三题 反转字符串中的元音字母
编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 2: 输入: "leet ...
- 345 Reverse Vowels of a String 反转字符串中的元音字母
编写一个函数,以字符串作为输入,反转该字符串中的元音字母.示例 1:给定 s = "hello", 返回 "holle".示例 2:给定 s = "l ...
- C#LeetCode刷题之#557-反转字符串中的单词 III(Reverse Words in a String III)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3955 访问. 给定一个字符串,你需要反转字符串中每个单词的字符顺 ...
- leetCode题解之反转字符串中的元音字母
1.问题描述 Reverse Vowels of a String Write a function that takes a string as input and reverse only the ...
- LeetCode 557:反转字符串中的单词 III Reverse Words in a String III
公众号:爱写bug(ID:icodebugs) 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. Given a string, you need to reve ...
随机推荐
- python3将字符串unicode转换为中文
在我们的python使用过程中,可能会遇到这样的情况: 我们得到的中文数据是unicode编码类型的,这在python中是没有问题的,可以直接打印显示为中文. 但是,如果我们需要和其它语言或前端进行交 ...
- day1 python计算器底层运作,注释及变量
每日一记 utf-8 国际标准编码(可变长的unicode编码)中文3字节,英文数字特殊字符1字节 gbk 中国标准编码 中文2字节,英文数字特殊字符1字节 1.原码,反码,补码 "&quo ...
- 想用@Autowired注入static静态成员?官方不推荐你却还偏要这么做
生命太短暂,不要去做一些根本没有人想要的东西.本文已被 https://www.yourbatman.cn 收录,里面一并有Spring技术栈.MyBatis.JVM.中间件等小而美的专栏供以免费学习 ...
- 深入浅出Semaphore源码解析
Semaphore通过permits的值来限制线程访问临界资源的总数,属于有限制次数的共享锁,不支持重入. 前提条件 在理解Semaphore时需要具备一些基本的知识: 理解AQS的实现原理 之前有写 ...
- swfupload控件文件上传大小限制设置
swfupload控件,是我在开发过程中用到的上传文件的控件,非常实用和方便.但最近碰到一些问题,解决之后进行一下整理. 因为用户上传文件的大小限制增加,导致原本上传控件时,文件的大小需要进行调整和限 ...
- HDU-2473 Junk-Mail Filter(并查集的使用)
原题链接:https://vjudge.net/problem/11782/origin Description: Recognizing junk mails is a tough task. Th ...
- vector基本用法
Vector作为STL容器中的一员,使用频率非常高,因此对其基本用法和实用技巧进行记录,便于后期查询使用. 基本用法 #include <iostream> #include <ve ...
- Python "按位或"和"按位异或"的区别
首先分别解释一下按位或和按位异或 按位或: 按位或指的是参与运算的两个数分别对应的二进制位进行“或”的操作.只要对应的两个二进制位有一个为1时,结果位就为1.python中运算符为“|” 按位异或: ...
- 软件测试必备技能,带你学习jmeter!
一:jmeter用户变量设置: 1.在线程组鼠标右击--添加--配置元件-用户定义的变量, 2.根据业务需求自定义变量的名称,添加需要的变量和对应的值 3.在脚本对应位置添加参数 二:文件参数化: 两 ...
- ~~网络编程(八):UDP~~
进击のpython ***** 网络编程--UDP 那现在看到这里的 这就是网络编程的最后一讲了 上面讲的都是关于TCP的编程方法 还记得TCP和UDP传输的区别吗? UDP简单就简单到它可以不借助管 ...