当前位置:网站首页>leetcode刷题_反转字符串中的元音字母
leetcode刷题_反转字符串中的元音字母
2022-07-06 01:19:00 【身影王座】
题目描述

Java解决方法
import java.util.*;
class Solution {
public String reverseVowels(String s) {
// ArrayList中有判断一个字符是否在一个数组中
Collection c = new ArrayList();
c.add('a');c.add('e');c.add('i');c.add('o');c.add('u');
c.add('A');c.add('E');c.add('I');c.add('O');c.add('U');
int head = 0;
int tail = s.length() - 1;
// 修改字符串中某个字符需要StringBuilder
StringBuilder s1 = new StringBuilder(s);
while(head < tail)
{
char a1 = s1.charAt(head);
char a2 = s1.charAt(tail);
if(!c.contains(a1))
{
head++;
}
else if(!c.contains(a2))
{
tail--;
}
else
{
s1.setCharAt(head, a2);
s1.setCharAt(tail, a1);
head++;
tail--;
}
}
String s2 = s1.toString();
return s2;
}
}
C语言解决方法
int nocontains(char a)
{
if(a != 'a' && a != 'A' && a != 'e' && a != 'E' && a != 'i' && a != 'I' && a != 'o' && a != 'O' && a != 'u' && a != 'U')
{
return 1;
}
return 0;
}
char * reverseVowels(char * s){
int head = 0;
int tail;
tail = strlen(s) - 1;
while(head < tail)
{
char a;
char b;
a = s[head];
b = s[tail];
if(nocontains(a))
{
head++;
}
else if(nocontains(b))
{
tail--;
}
else
{
s[head] = b;
s[tail] = a;
head++;
tail--;
}
}
return s;
}
边栏推荐
- Kotlin basics 1
- ThreeDPoseTracker项目解析
- golang mqtt/stomp/nats/amqp
- SPIR-V初窥
- 2020.2.13
- Mysql--- query the top 5 students
- China Taiwan strategy - Chapter 8: digital marketing assisted by China Taiwan
- Huawei converged VLAN principle and configuration
- Kotlin core programming - algebraic data types and pattern matching (3)
- Threedposetracker project resolution
猜你喜欢

yii中console方法调用,yii console定时任务
![Cf:h. maximum and [bit operation practice + K operations + maximum and]](/img/c2/9e58f18eec2ff92e164d8d156629cf.png)
Cf:h. maximum and [bit operation practice + K operations + maximum and]

Beginner redis

Hcip---ipv6 experiment

MATLB|实时机会约束决策及其在电力系统中的应用

基于DVWA的文件上传漏洞测试

Unity | 实现面部驱动的两种方式

View class diagram in idea

Questions about database: (5) query the barcode, location and reader number of each book in the inventory table

Pbootcms plug-in automatically collects fake original free plug-ins
随机推荐
What is the most suitable book for programmers to engage in open source?
Study diary: February 13, 2022
Condition and AQS principle
Three methods of script about login and cookies
Zhuhai laboratory ventilation system construction and installation instructions
About error 2003 (HY000): can't connect to MySQL server on 'localhost' (10061)
Huawei converged VLAN principle and configuration
Hundreds of lines of code to implement a JSON parser
黄金价格走势k线图如何看?
现货白银的一般操作方法
Installation and use of esxi
JMeter BeanShell的基本用法 一下语法只能在beanshell中使用
Mysql--- query the top 5 students
WGet: command line download tool
MATLB|实时机会约束决策及其在电力系统中的应用
File upload vulnerability test based on DVWA
The growth path of test / development programmers, the problem of thinking about the overall situation
Opinions on softmax function
Leetcode study - day 35
关于softmax函数的见解