345:反转字符串中的元音字母

This commit is contained in:
huangge1199@hotmail.com 2021-08-19 22:34:16 +08:00
parent 9dc8833ec1
commit 70fd653794
4 changed files with 102 additions and 2 deletions

View File

@ -0,0 +1,72 @@
//给你一个字符串 s 仅反转字符串中的所有元音字母并返回结果字符串
//
// 元音字母包括 'a''e''i''o''u'且可能以大小写两种形式出现
//
//
//
// 示例 1
//
//
//输入s = "hello"
//输出"holle"
//
//
// 示例 2
//
//
//输入s = "leetcode"
//输出"leotcede"
//
//
//
// 提示
//
//
// 1 <= s.length <= 3 * 105
// s 可打印的 ASCII 字符组成
//
// Related Topics 双指针 字符串
// 👍 209 👎 0
package leetcode.editor.cn;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
//345:反转字符串中的元音字母
class ReverseVowelsOfAString {
public static void main(String[] args) {
//测试代码
Solution solution = new ReverseVowelsOfAString().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String reverseVowels(String s) {
List<Character> list = new ArrayList<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
int start = 0;
int end = s.length() - 1;
char[] chs = s.toCharArray();
while (start < end) {
if (list.contains(chs[start]) && list.contains(chs[end])) {
char ch = chs[start];
chs[start] = chs[end];
chs[end] = ch;
start++;
end--;
}
if(!list.contains(chs[start])){
start++;
}
if(!list.contains(chs[end])){
end--;
}
}
return new String(chs);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,28 @@
<p>给你一个字符串 <code>s</code> ,仅反转字符串中的所有元音字母,并返回结果字符串。</p>
<p>元音字母包括 <code>'a'</code><code>'e'</code><code>'i'</code><code>'o'</code><code>'u'</code>,且可能以大小写两种形式出现。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>s = "hello"
<strong>输出:</strong>"holle"
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>s = "leetcode"
<strong>输出:</strong>"leotcede"</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 3 * 10<sup>5</sup></code></li>
<li><code>s</code><strong>可打印的 ASCII</strong> 字符组成</li>
</ul>
<div><div>Related Topics</div><div><li>双指针</li><li>字符串</li></div></div>\n<div><li>👍 209</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long