【Leetcode】Reverse Vowels of a String

题目链接:https://leetcode.com/problems/reverse-vowels-of-a-string/

题目:

Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:

Given s = "hello", return "holle".

Example 2:

Given s = "leetcode", return "leotcede".

思路:

easy

算法

public String reverseVowels(String s) {
       char c[] = s.toCharArray();
       Set<String> vowels = new HashSet<String>();
       vowels.add("a");
       vowels.add("e");
       vowels.add("i");
       vowels.add("o");
       vowels.add("u");
       vowels.add("A");
       vowels.add("E");
       vowels.add("I");
       vowels.add("O");
       vowels.add("U");
       int v1=-1,v2=-1;
       for(int start=0,end=c.length-1;start<end;){
        if(v1==-1){
            if(vowels.contains(c[start]+""))
                v1 = start;
            else
                start++;
        }
        if(v2==-1){
            if(vowels.contains(c[end]+""))
                v2 = end;
            else
                end--;
        }  

        if(v1!=-1&&v2!=-1){
            char tmp = c[v1];
            c[v1] = c[v2];
            c[v2] = tmp;
            v1 = -1;
            v2 = -1;
            start++;
            end--;
        }  

       }  

       return new String(c);
   }
时间: 2024-10-11 01:50:48

【Leetcode】Reverse Vowels of a String的相关文章

【leetcode】Reverse Words in a String

问题:给定一个字符串,字符串中包含若干单词,每个单词间由空格分隔,将单词逆置,即第一个单词成为最后一个单词,一次类推. 说明:字符串本身可能包含前导空格或后导空格,单词间可能包含多个空格,要求结果中去掉前导和后导空格,单词间空格只保留一个. 与rotate函数类似,先逆置每个单词,再将所有字符串逆置. void reverseWords(string &s) { if(s.size() == 0) return; char blank = ' '; size_t len = s.size();

【leetcode】Reverse Words in a String (python)

陆陆续续几个月下来,终于把题刷完了,过程中遇到的python的题解很少,这里重新用python实现下,所以题解可能都是总结性的,或者是新的心得,不会仅针对题目本身说的太详细. def reverseWords(self, s): s = ' '.join(s.split()[::-1]) return s [ : :  -1 ] 是将元素进行翻转 [leetcode]Reverse Words in a String (python),布布扣,bubuko.com

【leetcode80】Reverse Vowels of a String(元音字母倒叙)

题目描述: 写一个函数,实现输入一个字符串,然后把其中的元音字母倒叙 注意 元音字母包含大小写,元音字母有五个a,e,i,o,u 原文描述: Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Given s = "hello", return "holle". Example 2: Given s = "leet

【Leetcode】Reverse Words in a String JAVA实现

一.题目描述 Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". 二.分析 要注意几点:1.当字符串的头部或者尾部存在空格时,最后都将被消除 2.当两个子字符串之间的空格的个数大于1时,只要保留一个 解题思路:1.首先,将整个字符串进行反转 2.然后,使用split函数对字符串

【leetcode】Reverse Words in a String(hard)☆

Given an input string, reverse the string word by word. For example,Given s = "the sky is blue",return "blue is sky the". For C programmers: Try to solve it in-place in O(1) space. Clarification: What constitutes a word?A sequence of n

【LeetCode】Reverse Integer (2 solutions)

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alread

【LeetCode】Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

【leetcode】reverse Nodes in k-groups

问题: 给定一个链表的头指针,以及一个整数k,要求将链表按每k个为一组,组内进行链表逆置.少于k个的部分不做处理. 分析: 个人觉得问题的重点是熟悉链表的就地逆置操作,就是头插法.其他的考察点如果还有的话,就的细心程度. 实现: void reverseList(ListNode *&pre, ListNode *head) { ListNode *tail = NULL; while (head) { ListNode* next = head->next; head->next =

【LeetCode】Reverse Linked List II 解题报告

[题目] Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n