当前位置:网站首页>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;
}
边栏推荐
- 毕设-基于SSM高校学生社团管理系统
- Leetcode daily question solution: 1189 Maximum number of "balloons"
- SSH login is stuck and disconnected
- JMeter BeanShell的基本用法 一下语法只能在beanshell中使用
- Remember that a version of @nestjs/typeorm^8.1.4 cannot be obtained Env option problem
- ORA-00030
- Five challenges of ads-npu chip architecture design
- Leetcode1961. 检查字符串是否为数组前缀
- Blue Bridge Cup embedded stm32g431 - the real topic and code of the eighth provincial competition
- cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】
猜你喜欢
基于DVWA的文件上传漏洞测试
基於DVWA的文件上傳漏洞測試
有谁知道 达梦数据库表的列的数据类型 精度怎么修改呀
[pat (basic level) practice] - [simple mathematics] 1062 simplest fraction
How to see the K-line chart of gold price trend?
Mobilenet series (5): use pytorch to build mobilenetv3 and learn and train based on migration
Leetcode study - day 35
The inconsistency between the versions of dynamic library and static library will lead to bugs
File upload vulnerability test based on DVWA
VSphere implements virtual machine migration
随机推荐
Four commonly used techniques for anti aliasing
Blue Bridge Cup embedded stm32g431 - the real topic and code of the eighth provincial competition
视频直播源码,实现本地存储搜索历史记录
Cve-2017-11882 reappearance
Hundreds of lines of code to implement a JSON parser
Mathematical modeling learning from scratch (2): Tools
Huawei converged VLAN principle and configuration
Opinions on softmax function
[day 30] given an integer n, find the sum of its factors
Spir - V premier aperçu
WGet: command line download tool
How to get the PHP version- How to get the PHP Version?
Fibonacci number
Cglib dynamic agent -- example / principle
Leetcode 208. 实现 Trie (前缀树)
Leetcode daily question solution: 1189 Maximum number of "balloons"
MYSQL---查询成绩为前5名的学生
Cf:c. the third problem
Paging of a scratch (page turning processing)
[pat (basic level) practice] - [simple mathematics] 1062 simplest fraction