当前位置:网站首页>343-Leetcode 反转字符串中的元音字母
343-Leetcode 反转字符串中的元音字母
2022-04-22 15:54:00 【sp_13230409636】

方法一:双指针
class Solution
{
public:
string reverseVowels(string s)
{
unordered_map<char, int> map
{
{
'a',1},
{
'e',1 },
{
'i',1 },
{
'o',1 },
{
'u',1 },
{
'A',1 },
{
'E',1 },
{
'I',1},
{
'O',1 },
{
'U',1 },
};
int len = s.size();
for (int i = 0, j = len - 1; i < j; ++i, --j)
{
while (i < j && map[s[i]] != 1)
{
++i;
}
while (i < j && map[s[j]] != 1)
{
--j;
}
if (i < j)
{
swap(s[i], s[j]);
}
}
return s;
}
};
int main()
{
Solution A;
cout << A.reverseVowels("hello") << endl;
return 0;
}
时间复杂度:O(n),其中 n 是字符串 s 的长度。在最坏的情况下,两个指针各遍历整个字符串一次
空间复杂度:O(1) 或 O(n),取决于使用的语言中字符串类型的性质。如果字符串是可修改的,那么我们可以直接在字符串上使用双指针进行交换,空间复杂度为 O(1),否则需要使用 O(n) 的空间将字符串临时转换为可以修改的数据结构(例如数组),空间复杂度为 O(n)
版权声明
本文为[sp_13230409636]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_45964837/article/details/124341900
边栏推荐
- 想做自媒体运营却不会写作?4个珍藏的运营技巧
- [in depth understanding of tcallusdb technology] example code for deleting data - [generic table]
- 这个API Hub厉害了,收录了钉钉企业微信等开放Api,还能直接调试 !
- Displays whether it is urgent
- jsp回显
- 太卷了~(2022版)大厂面经 + 详细笔记帮你搞定面试
- leetcode-----奇偶树
- Vidéo hebdomadaire recommandée: comment reconstruire les compétences de base des entreprises à l'ère des stocks?
- Sign up to open QKE container engine hosting version and container ecological Conference!
- 函数指针与回调函数
猜你喜欢

制作的自媒体短视频很模糊?教你3个方法,让视频变清晰

太卷了~(2022版)大厂面经 + 详细笔记帮你搞定面试

【Altium Designer10详细安装】

Wukong's private collection of "architecture" books recommended

Dongfeng Nissan recalls some Xiaoke with potential safety hazards
理想与集度的技术之争:激光雷达究竟装哪儿更安全?

Zero negative comment on the reputation of Jihu products BAIC Blue Valley has captured the hearts of users with high-end hard core strength

这个陀螺仪的精度太高了,还是建议禁止使用吧。

Reverse linked list (and) the intermediate node of the linked list

哈希表篇(二)
随机推荐
Radio button selected
Servlet基础
How to input multi line string in yaml?
Huawei cloud media Zha Yong: Huawei cloud's technical practice in the field of video AI transcoding
sql語句———多錶聯查
eltable样式修改。父子组件传值。模糊查询
redis常见面试题有哪些?redis集群面试题及答案整理
AI智能视频技术如何应用在文物古迹建筑日常养护监管场景中?
做短视频自媒体挣不到Q?教你6招
近期BSN开发常见问题答疑
使用js完成文字根据输入框内数字在屏幕上移动
引入文件路径问题-$_SERVER[‘DOCUMENT_ROOT‘]代表网站根目录
面试官:请说说==操作符和equals(),从基本数据类型和引用数据类型两个角度谈谈
设置json编码
Redis thread model
极狐产品口碑零差评 北汽蓝谷以高端硬核实力俘获用户芳心
Build your own web site (8)
Spark basic learning notes 23: dataframe and dataset
Grafana 系列文章(十三):如何用 Loki 收集查看 Kubernetes Events
一个页面向同一个地方提交两个form表单