当前位置:网站首页>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;
}
边栏推荐
- Mathematical modeling learning from scratch (2): Tools
- servlet(1)
- Finding the nearest common ancestor of binary search tree by recursion
- Redis' cache penetration, cache breakdown, cache avalanche
- Cf:h. maximum and [bit operation practice + K operations + maximum and]
- Vulhub vulnerability recurrence 74_ Wordpress
- Obstacle detection
- 95后CV工程师晒出工资单,狠补了这个,真香...
- Netease smart enterprises enter the market against the trend, and there is a new possibility for game industrialization
- Introduction to robotics I. spatial transformation (1) posture, transformation
猜你喜欢
Convert binary search tree into cumulative tree (reverse middle order traversal)
程序员搞开源,读什么书最合适?
普通人下场全球贸易,新一轮结构性机会浮出水面
Vulhub vulnerability recurrence 74_ Wordpress
What is the most suitable book for programmers to engage in open source?
servlet(1)
Five challenges of ads-npu chip architecture design
ubantu 查看cudnn和cuda的版本
yii中console方法调用,yii console定时任务
cf:D. Insert a Progression【关于数组中的插入 + 绝对值的性质 + 贪心一头一尾最值】
随机推荐
Kotlin core programming - algebraic data types and pattern matching (3)
Dedecms plug-in free SEO plug-in summary
朝招金安全吗 会不会亏损本金
Who knows how to modify the data type accuracy of the columns in the database table of Damon
What is the most suitable book for programmers to engage in open source?
Zhuhai laboratory ventilation system construction and installation instructions
Cve-2017-11882 reappearance
cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】
Unity | two ways to realize facial drive
SCM Chinese data distribution
golang mqtt/stomp/nats/amqp
Mysql--- query the top 5 students
What is weak reference? What are the weak reference data types in ES6? What are weak references in JS?
Cglib dynamic agent -- example / principle
毕设-基于SSM高校学生社团管理系统
Programmer growth Chapter 9: precautions in real projects
Leetcode 208. Implement trie (prefix tree)
Dynamic programming -- linear DP
Gartner released the prediction of eight major network security trends from 2022 to 2023. Zero trust is the starting point and regulations cover a wider range
Crawler request module