首页 > 其他分享 >leetcode-345-easy

leetcode-345-easy

时间:2023-01-08 19:58:03浏览次数:60  
标签:set String 345 add right easy array leetcode left

Reverse Vowels of a String

Given a string s, reverse only all the vowels in the string and return it.

The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once.

Example 1:

Input: s = "hello"
Output: "holle"
Example 2:

Input: s = "leetcode"
Output: "leotcede"
Constraints:

1 <= s.length <= 3 * 105
s consist of printable ASCII characters.

思路一:双指针,先定位,然后交换指针字符

    public String reverseVowels(String s) {
        Set<Character> set = new HashSet<>();
        set.add('a');set.add('A');
        set.add('e');set.add('E');
        set.add('i');set.add('I');
        set.add('o');set.add('O');
        set.add('u');set.add('U');

        char[] array = s.toCharArray();
        int left = 0;
        int right = array.length - 1;


        while (left < right) {
            while (left < right && !set.contains(array[left])) {
                left++;
            }

            while (left < right && !set.contains(array[right])) {
                right--;
            }

            if (left < right) {
                char t = array[left];
                array[left] = array[right];
                array[right] = t;

                left++; right--;
            }
        }

        return new String(array);
    }

标签:set,String,345,add,right,easy,array,leetcode,left
From: https://www.cnblogs.com/iyiluo/p/17035190.html

相关文章

  • leetcode-643-easy
    MaximumAverageSubarrayIYouaregivenanintegerarraynumsconsistingofnelements,andanintegerk.Findacontiguoussubarraywhoselengthisequalto......
  • leetcode-496-easy
    NextGreaterElementIThenextgreaterelementofsomeelementxinanarrayisthefirstgreaterelementthatistotherightofxinthesamearray.Youar......
  • leetcode-521-easy
    LongestUncommonSubsequenceIGiventwostringsaandb,returnthelengthofthelongestuncommonsubsequencebetweenaandb.Ifthelongestuncommonsubseq......
  • leetcode-551-easy
    StudentAttendanceRecordIYouaregivenastringsrepresentinganattendancerecordforastudentwhereeachcharactersignifieswhetherthestudentwasab......
  • leetcode-485-easy
    MaxConsecutiveOnesGivenabinaryarraynums,returnthemaximumnumberofconsecutive1'sinthearray.Example1:Input:nums=[1,1,0,1,1,1]Output:3E......
  • leetcode-434-easy
    NumberofSegmentsinaStringGivenastrings,returnthenumberofsegmentsinthestring.Asegmentisdefinedtobeacontiguoussequenceofnon-spacech......
  • 【LeetCode数组#4】长度最小的子数组
    长度最小的子数组力扣题目链接(opensnewwindow)给定一个含有n个正整数的数组和一个正整数s,找出该数组中满足其和≥s的长度最小的连续子数组,并返回其长度。如......
  • [leetcode每日一题]1.8
    ​​2185.统计包含给定前缀的字符串​​难度简单28给你一个字符串数组 ​​words​​ 和一个字符串 ​​pref​​ 。返回 ​​words​​ 中以 ​​pref​​ 作为 ......
  • 【优先队列】LeetCode 23. 合并K个升序链表
    题目链接23.合并K个升序链表思路把全部结点放入优先队列中,然后再依次组成新链表代码classSolution{publicListNodemergeKLists(ListNode[]lists){......
  • LeetCode 236_二叉树的最近公共祖先
    LeetCode236:二叉树的最近公共祖先题目给定一个二叉树,找到该树中两个指定节点的最近公共祖先。百度百科中最近公共祖先的定义为:“对于有根树T的两个节点p、q,最近......