问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章

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

  2. Java实现 LeetCode 345 反转字符串中的元音字母

    345. 反转字符串中的元音字母 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 ...

  3. Leetcode 345. 反转字符串中的元音字母 By Python

    编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 2: 输入: "leet ...

  4. LeetCode:反转字符串中的元音字母【345】

    LeetCode:反转字符串中的元音字母[345] 题目描述 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "h ...

  5. 【leetcode 简单】 第八十三题 反转字符串中的元音字母

    编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 2: 输入: "leet ...

  6. 345 Reverse Vowels of a String 反转字符串中的元音字母

    编写一个函数,以字符串作为输入,反转该字符串中的元音字母.示例 1:给定 s = "hello", 返回 "holle".示例 2:给定 s = "l ...

  7. C#LeetCode刷题之#557-反转字符串中的单词 III(Reverse Words in a String III)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3955 访问. 给定一个字符串,你需要反转字符串中每个单词的字符顺 ...

  8. leetCode题解之反转字符串中的元音字母

    1.问题描述 Reverse Vowels of a String Write a function that takes a string as input and reverse only the ...

  9. LeetCode 557:反转字符串中的单词 III Reverse Words in a String III

    公众号:爱写bug(ID:icodebugs) 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. Given a string, you need to reve ...

随机推荐

  1. python3将字符串unicode转换为中文

    在我们的python使用过程中,可能会遇到这样的情况: 我们得到的中文数据是unicode编码类型的,这在python中是没有问题的,可以直接打印显示为中文. 但是,如果我们需要和其它语言或前端进行交 ...

  2. Go的100天之旅-常量

    常量 简介 道可道,非常道.这里常道指的永恒不变的道理,常有不变的意思.顾名思义和变量相比,常量在声明之后就不可改变,它的值是在编译期间就确定的. 下面简单的声明一个常量: const p int = ...

  3. Go Pentester - HTTP Servers(3)

    Building Middleware with Negroni Reasons use middleware, including logging requests, authenticating ...

  4. Qt-数据库操作SQLite

    1  简介 参考视频:https://www.bilibili.com/video/BV1XW411x7NU?p=88 说明:本文对在Qt中操作SQLite做简要说明. SQLite:SQLite 是 ...

  5. 【最短路+bfs+缩点】Paint the Grid Reloaded ZOJ - 3781

    题目: Leo has a grid with N rows and M columns. All cells are painted with either black or white initi ...

  6. SQL数据多条转单条(CONCAT_WS)

    一.concat()函数可以连接一个或者多个字符串 concat(str1,str2,…) 返回结果为连接参数产生的字符串.如有任何一个参数为NULL ,则返回值为 NULL. select conc ...

  7. python学习之路------你想要的都在这里了

    python学习之路------你想要的都在这里了 (根据自己的学习进度后期不断更新哟!!!) 一.python基础 1.python基础--python基本知识.七大数据类型等 2.python基础 ...

  8. flask中url_for使用endpoint和视图函数名

    在flask中,使用url_for 进行路由反转时,需要传递一个endpoint的值,用法如下: @app.route('/', endpoint='my_index') def index(): r ...

  9. Redis之Redis入门介绍

    1.Redis概述    所谓Redis全称为REmote DIctionary Server(远程字典服务器) 是完全开源免费的,用C语言编写的,遵守BSD协议,是一个高性能的(key/value) ...

  10. 做完这套面试题,你才敢说懂Excel

    下面的题目来自一份商品专员的面试题,其中有涉及到条件格式.自定义排序.数据验证制作下拉菜单.查找引用类函数.文本提取函数等等技能. 满满的干货技能可不是商品专员“专属”,如果你能熟练掌握,在平日工作中 ...