[每日算法 - 华为机试] leetcode345 :反转字符串中的元音字母「双指针」
入口
力扣
https://leetcode.cn/problems/reverse-vowels-of-a-string/submissions/
题目描述
给你一个字符串 s ,仅反转字符串中的所有元音字母,并返回结果字符串。
元音字母包括 'a'、'e'、'i'、'o'、'u',且可能以大小写两种形式出现不止一次。
示例 1:
输入:s = "hello"
输出:"holle"
示例 2:输入:s = "leetcode"
输出:"leotcede"
方法一:双指针
class Solution {
public String reverseVowels(String s) {
int n = s.length();
int i=0,j=n-1;
char[] arr = s.toCharArray();
while(i<j){
//注意时while而不是if
while(i<j && !isVoel(arr[i])){
++i;
}
while(i<j && !isVoel(arr[j])){
--j;
}
if(i<j){
swap(arr,i,j);
//!!交换完成后收缩窗口
++i;
--j;
}
}
return new String(arr);
}
public boolean isVoel(char c){
return "aeiouAEIOU".indexOf(c) >= 0;
}
public void swap(char[] arr,int i,int j){
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
[每日算法 - 华为机试] leetcode345 :反转字符串中的元音字母「双指针」的更多相关文章
- [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 ...
- LeetCode:反转字符串中的元音字母【345】
LeetCode:反转字符串中的元音字母[345] 题目描述 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "h ...
- Java实现 LeetCode 345 反转字符串中的元音字母
345. 反转字符串中的元音字母 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 ...
- Leetcode 345. 反转字符串中的元音字母 By Python
编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 2: 输入: "leet ...
- 【leetcode 简单】 第八十三题 反转字符串中的元音字母
编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 2: 输入: "leet ...
- 345 Reverse Vowels of a String 反转字符串中的元音字母
编写一个函数,以字符串作为输入,反转该字符串中的元音字母.示例 1:给定 s = "hello", 返回 "holle".示例 2:给定 s = "l ...
- leetCode题解之反转字符串中的元音字母
1.问题描述 Reverse Vowels of a String Write a function that takes a string as input and reverse only the ...
- Python实现 "反转字符串中的元音字母" 的方法
#coding=utf- def reverseVowels(s): """ :type s: str :rtype: str """ sS ...
- C#LeetCode刷题之#345-反转字符串中的元音字母(Reverse Vowels of a String)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3935 访问. 编写一个函数,以字符串作为输入,反转该字符串中的元 ...
- LeetCode--345--反转字符串中的元音字母
问题描述: 编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 2: 输入: &quo ...
随机推荐
- Python 进阶:深入理解 import 机制与 importlib 的妙用
大家好,今天我们来深入探讨 Python 中的导入机制和 importlib 模块.相信不少朋友和我一样,平时写代码时可能只用过最基础的 import 语句,或者偶尔用 importlib.impor ...
- 架构-初识DDD
引言 继上一篇BFF的文章后,我又去网上学习了一下DDD(领域驱动设计),发现一篇不错的文章,参考并写了一些自己的理解分享在这里. DDD 是什么 领域驱动设计(Domain Driven Desig ...
- c# 通过注册表判断有没有安装某个软件
private bool checkHasInstalledSoftWare(string displayName) { Microsoft.Win32.RegistryKey uninstallNo ...
- c# Progress<T>
c# Progress<T> 用于显示进度........主要是利用IProgress<T> 的Report(T)方法: private void BtnDownload_Cl ...
- 如何快速的开发一个完整的iOS直播app(创建房间)
直播(创建房间) 1.进入主播界面,首先创建房间 2.设计房间模型(key,名称),key作为房间的唯一标识,用来找到房间 3.用socket创建房间,导入socket.io框架 4.一般一个客户端一 ...
- 多方安全计算(6):MPC中场梳理
学习&转载文章:多方安全计算(6):MPC中场梳理 前言 诚为读者所知,数据出域的限制约束与数据流通的普遍需求共同催生了数据安全计算的需求,近一两年业界又统将能够做到多方数据可用不可见的技术归 ...
- shell 变量的运算、保存硬件信息脚本
变量的数学运算 方法一:expr ##加减乘除 [root@localhost ~]# num1=10[root@localhost ~]# num2=16[root@localhost ~]# ex ...
- 使用Docker编译安装运行Doris
一.编译源码 (1)拉取编译镜像docker pull apache/incubator-doris:build-env-1.2 (2)Mac电脑上拉取源码git clone https://gith ...
- VSCode Romote SSH连接远程主机经常初始化?10个解决方法!!
解决方法: 删除远程主机上的 .vscode-server 文件夹:这个文件夹包含 VSCode 的远程服务器组件.如果这个文件夹损坏或配置不正确,会导致连接问题.删除并重新安装可以确保服务器组件是干 ...
- LINUX 服务器安装nginx redis jdk等步聚
1.安装指令步聚 sudo yum update 更新linux系统 yum install -y nginx 安装nginx systemctl enable nginx 设置开机启动nginx s ...