Leetcode 345: Reverse Vowels of a String
问题描述:
Given a string s, reverse only all the vowels in the string and return it.
The vowels are ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’, and they can appear in both cases.
s consist of printable ASCII characters
将原字符串中的元音字母倒序
注意审题,A与a均为元音,所以要考虑大小写
思路:同样是双指针夹击,当左侧指针遇到元音字母时,停住并设立stopSign标记,否则前进;同理右侧指针。若在某一时刻两侧指针均被stopSign标记,这时需要交换,交换后解除stopSign,并继续靠近。
遇到的问题:没审清题,未考虑元音字母存在的大小写问题
代码如下:
注:用stringbuilder方便交换字符串中的元素
public String reverseVowels(String s) {int left=0;int right=s.length()-1;char temp = ' ';StringBuilder ans = new StringBuilder(s);boolean stopSign_left = false;boolean stopSign_right = false;//when any pointer detects a vowel, stop, and do not move until the other pointer detects a vowelwhile(left<right){if (isVowel(ans.charAt(left))){stopSign_left = true;}else{left++;}if (isVowel(ans.charAt(right))){stopSign_right = true;}else{right--;}if ((stopSign_left)&&(stopSign_right)){temp = ans.charAt(left);ans.setCharAt(left, s.charAt(right));ans.setCharAt(right, temp);stopSign_left = false;stopSign_right = false;left++;right--;}}return ans.toString();}public boolean isVowel(char something){if ((something=='a')||(something=='e')||(something=='i')||(something=='o')||(something=='u')||(something=='A')||(something=='E')||(something=='I')||(something=='O')||(something=='U')){return true;}return false;}
}
时间复杂度:O(n)
Leetcode 345: Reverse Vowels of a String相关推荐
- 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 1 ...
- 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 字符串处理
题意:倒置字符串中的元音字母. 用两个下标分别指向前后两个相对的元音字母,然后交换. 注意:元音字母是aeiouAEIOU. 1 class Solution { 2 public: 3 bool i ...
- 345. Reverse Vowels of a String - LeetCode
Question 345. Reverse Vowels of a String Solution 思路:交换元音,第一次遍历,先把出现元音的索引位置记录下来,第二遍遍历元音的索引并替换. Java实 ...
- 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 ...
- 【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 1 ...
- 【LeetCode】345. Reverse Vowels of a String 解题报告
转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51429823 Subject 出处:https://leetcode.com/prob ...
- 345. Reverse Vowels of a String
题目: Given a string s, reverse only all the vowels in the string and return it. The vowels are 'a', ' ...
- 345. Reverse Vowels of a String(python+cpp)
题目: Write a function that takes a string as input and reverse only the vowels of a string. Example 1 ...
最新文章
- (转)记忆杭州中的(非杭户籍人)
- oracle 11g 逻辑备库,通过Oracle 11g 逻辑standby实现BI的需求
- 边缘计算的三种模式:MEC、微云和雾计算
- Java 添加音频到ppt_Java 添加文本框到PPT幻灯片过程解析
- Java 回调函数的理解
- python 函数教程:必选参数与默认参数
- oracle的md5加密
- Maven系列二setting.xml 配置详解
- Serial Old收集器
- 快速掌握MATLAB应用,只要从这一步开始!
- LeetCode 5.最长回文子串(动态规划)
- Linux 增加交换分区
- PHP文件中定义加载资源文件
- 阶段3 1.Mybatis_08.动态SQL_01.mybatis中的动态sql语句-if标签
- 在Windows系统搭建.NET Core环境并创建运行ASP.NET网站
- 你的计算机没有安装cad2006,win7安装autocad2006无法正常的2种解决方法
- Vijos 1048 送给圣诞夜的贺卡(dfs)
- 记一次大量499http状态码问题出现与处理
- 数据埋点日志awk脚本快速入库
- 科技云报道:率先迈入“10微秒”时代,腾讯云存储跑出加速度
热门文章
- 例3.2、计算存款利息。有1000元,想存一年。有三种方法可选:(1)活期,年利率为r1;(2)一年期定期,年利率为r2;(3)存两次半年定期,年利率为r3。请分别计算出一年后按3种方法所得到的本息和
- onenote同时移动图片和绘图
- 小米隔空充电技术,肌肉还是鸡肋?
- python中arcsec_Python中jpg图像的显示网格
- 计算机硬盘做u盘启动不了,u盘启动盘读不出来硬盘如何解决_电脑U盘启动找不到硬盘的解决教程...
- FireFox插件 - Greasemonkey
- 戴尔dell PowerEdge R440安装redhat操作系统
- java循环屏障,Java并发编程系列23|循环屏障CyclicBarrier
- 找出字符串中第一个不重复的字符
- 电脑内存占用过高怎么解决?