您好,欢迎来到飒榕旅游知识分享网。
搜索
您的当前位置:首页Two pointers (3) -- Merge Sorted Array, Reverse Vowels of a String

Two pointers (3) -- Merge Sorted Array, Reverse Vowels of a String

来源:飒榕旅游知识分享网
Merge Sorted Array

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

Note:
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.


1. 先找到两个数组合并后的最大值并放在其在nums1中的正确位置,然后依次类推。

2. 因此nums1数组头部是有值的,如果先确定头部的值,会较多的元素移动的操作。
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
        int flag2 = n-1;
        if(m == 0){
            nums1.swap(nums2);
            return;
        }
        for(int i = m - 1; i > -1; i--){
            for(int j = flag2; j > -1; j--){
                if(nums1[i] > nums2[j]){
                    nums1[i+j+1] = nums1[i];
                    break;
                }
                else{
                    nums1[i+j+1] = nums2[j];
                    flag2--;
                }
            }
        }
        for(int i = 0; i < flag2 + 1; i++){
            nums1[i] = nums2[i];
        }
    }
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".

Note:
The vowels does not include the letter "y".

解法1:双指针,如果都是元音则进行交换。但是这种解法特别慢..
    string reverseVowels(string s) {
        if(s.size() == 0) return s;
        unordered_set<char> vowels{'a','e','i','o','u','A','E','I','O','U'};
        int start = 0;
        int end = s.length()-1;
        while(start<end){
            while(vowels.find(s[start]) == vowels.end() && start < end){
                start++;
            }
            while(vowels.find(s[end]) == vowels.end() && start < end){
                end--;
            }
            swap(s[start++], s[end--]);
        }
        return s;
    }
这种写法就稍快一些,为啥。。
    string reverseVowels(string s) {
        auto p1 = s.begin(), p2 = s.end() - 1;
        string vowels = "aeiouAEIOU";
        while(p1 < p2) {
            while((vowels.find(*p1) == string::npos) && (p1 < p2)) p1++;
            while((vowels.find(*p2) == string::npos) && (p1 < p2)) p2--;
            if(p1 < p2) swap(*p1, *p2);
            p1++;
            p2--;
        }
        return s;
    }



解法2:使用find_first_of 和 find_last_of. 此解法非常快。以下摘自:


find_first_of()函数: 
查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,如果没找到就返回string::npos 
查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到就返回string::npos, 
查找在字符串中第一个与ch匹配的字符,返回它的位置。搜索从index开始

    string reverseVowels(string s) {
        int i = 0, j = s.size() - 1;
        while (i < j) {
            i = s.find_first_of("aeiouAEIOU", i);
            j = s.find_last_of("aeiouAEIOU", j);
            if (i < j) {
                swap(s[i++], s[j--]);
            }
        }
        return s;
    }













因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- sarr.cn 版权所有 赣ICP备2024042794号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务