当前位置:网站首页>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;
}
边栏推荐
- Cf:d. insert a progression [about the insert in the array + the nature of absolute value + greedy top-down]
- cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】
- 面试必刷算法TOP101之回溯篇 TOP34
- servlet(1)
- Basic process and testing idea of interface automation
- RAID disk redundancy queue
- Unity VR resource flash surface in scene
- Fibonacci number
- Some features of ECMAScript
- C language programming (Chapter 6 functions)
猜你喜欢

3D模型格式汇总

After 95, the CV engineer posted the payroll and made up this. It's really fragrant

Vulhub vulnerability recurrence 74_ Wordpress

Recursive method to realize the insertion operation in binary search tree

Study diary: February 13, 2022

Loop structure of program (for loop)

Unity | two ways to realize facial drive

Leetcode study - day 35

Installation and use of esxi

How to see the K-line chart of gold price trend?
随机推荐
Four commonly used techniques for anti aliasing
JVM_ 15_ Concepts related to garbage collection
Tcpdump: monitor network traffic
cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】
Distributed base theory
VSphere implements virtual machine migration
Cf:d. insert a progression [about the insert in the array + the nature of absolute value + greedy top-down]
ORA-00030
Leetcode sword finger offer 59 - ii Maximum value of queue
Four dimensional matrix, flip (including mirror image), rotation, world coordinates and local coordinates
朝招金安全吗 会不会亏损本金
在产业互联网时代,将会凭借大的产业范畴,实现足够多的发展
2020.2.13
Leetcode 剑指 Offer 59 - II. 队列的最大值
Docker compose配置MySQL并实现远程连接
Opinions on softmax function
Huawei converged VLAN principle and configuration
Development trend of Ali Taobao fine sorting model
Yii console method call, Yii console scheduled task
yii中console方法调用,yii console定时任务