leetcode_345 Reverse Vowels of a String
题目分析:
给定一个字符串,把字符串中的元音字符转置
解题思路:
1)定义两个指标i,j,分别指向字符串最前端和字符串最尾端;
2)遍历字符串,判断字符串中对应位置字符是否为元音字符,如果是元音字符,停止遍历,然后在另一方向找到对应位置的元音字符位置,找到后,将两个位置字符进行交换,然后继续遍历,直到遍历结束。
3)遍历方式类似于快速排序中的遍历调整位置。
实现程序
C++版本
class Solution {public:void my_swap(char *c1, char *c2){char temp = *c1;*c1 = *c2;*c2 = temp;}string reverseVowels(string s){int i = 0;int j = s.length() - 1;while (i < j){while (i < j && (s[i] != 'a' && s[i] != 'e' && s[i] != 'i' && s[i] != 'o' && s[i] != 'u')&& (s[i] != 'A' && s[i] != 'E' && s[i] != 'I' && s[i] != 'O' && s[i] != 'U'))i++;while (i < j && (s[j] != 'a' && s[j] != 'e' && s[j] != 'i' && s[j] != 'o' && s[j] != 'u')&& (s[j] != 'A' && s[j] != 'E' && s[j] != 'I' && s[j] != 'O' && s[j] != 'U'))j--;my_swap(&s[i], &s[j]);i++;j--;}return s;} };
Java版本
public boolean isVowels(char c){// 将字符c同一转换为小写字符c = Character.toLowerCase(c);if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')return true;return false;}public String reverseVowels(String s){int i = 0;int j = s.length() - 1;char[] ss = s.toCharArray();char temp;while (i < j){while (i < j && (!isVowels(ss[i]))){i++;}while (i < j && (!isVowels(ss[j]))){j--;}// 交换两个ss[i]与ss[j]temp = ss[i];ss[i] = ss[j];ss[j] = temp;i++;j--;}return new String(ss);}
leetcode_345 Reverse Vowels of a String相关推荐
- Reverse Vowels of a String (反转字符串中的母音)
leetcode Reverse Vowels of a String 反转字符串中的母音 一.学习要点: 1.find_first_of:查找与字符串str中某个字符相同的位置,并返回他的第一个出现 ...
- 345. Reverse Vowels of a String - LeetCode
Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...
- LeetCode345. Reverse Vowels of a String
345. Reverse Vowels of a String My Submissions QuestionEditorial Solution Total Accepted: 3821 Total ...
- 345.反转字符串中的元音字母(Reverse Vowels of a String)
题目描述 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 给定 s = "hello", 返回 "holle". 示例 2: 给定 s ...
- 从零开始的LC刷题(74): Reverse Vowels of a String
原题: Write a function that takes a string as input and reverse only the vowels of a string. Example 1 ...
- LeetCode:345. Reverse Vowels of a String
051103 题目 Write a function that takes a string as input and reverse only the vowels of a string. Exa ...
- leetcode345——Reverse Vowels of a String(C++)
Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Gi ...
- Reverse Vowels of a String
Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Gi ...
- LeetCode Reverse Vowels of a String
原题链接在这里:https://leetcode.com/problems/reverse-vowels-of-a-string/ 题目: Write a function that takes a ...
最新文章
- 腾讯博士:年薪60万辞职去教书,你想要哪种生活?
- Android -- View移动的六种方法
- SAP MM MIGO + 311 针对预留单做转库不能修改目的地存储地点!
- AlertDialog显示错误 Unable to add window token null is not for an application
- envi反演水质参数_科技前沿基于GOCI静止水色卫星数据的长江口及邻近海域Kd(490)遥感反演及其在机载激光测深预评估中的应用...
- java 6位日期 0001,当日期范围更改为从01-01-0001开始时,DimDate将不会加载
- MySQL查询select实例 【笔记】
- 我们一定要有自立的飞秋觉悟
- 看完这篇,终于知道自己会不会 C# 泛型了!
- Python教学视频(六)关系及逻辑运算
- linux u盘 引导修复工具下载,全能u盘恢复工具
- 产品结构图 = 产品功能结构图 + 产品信息结构图
- 无情刀永不知错,无缘分只叹奈何
- 以太坊区块链快速入门
- 深入理解RPC-RPC要解决的核心问题和在企业服务中的地位
- Entry键值对对象
- 【L2-020 功夫传人】天梯赛L2系列详解
- 【动画】背景图片旋转(内容不旋转)
- ubuntu18.04安装腾达无线网卡TendaU12驱动
- 2018年,请继续燃烧你的梦想
热门文章
- 数据分析师是如何被淘汰的?
- PHP汉字转拼音笔记.txt
- ErrorException : Use of undefined constant LARAVEL_START - assumed 'LARAVEL_START'
- matlab 手把手教你制作五子棋小游戏
- matlab机器人工具箱学习笔记——ikine函数
- mkfs fat32 卷标 linux,fdisk_mkfs分区格式化课件.pdf
- linux 下strip工具,linux下使用strip如何对库和可执行文件进行裁减
- C# 学习笔记 1.初识
- 网络营销分析七大法宝介绍
- pytorch系统学习