leetcode ---双指针+滑动窗体

一:Minimum Size
Subarray Sum
(最小长度子数组的和O(N))

题目:

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn‘t one, return
0 instead.

For example, given the array [2,3,1,2,4,3] and s
= 7
,

the subarray [4,3] has the minimal length under the problem constraint.

分析:一開始我採用的是LIS(longest increased sequence)中的最长递增子序列中的动态规划的思想。能通过,可是时间复杂度为O(N

^2);。;另外一种方法是採用双指针+滑动窗体的思想。时间复杂度为O(N)。 严格意义上说是2N。,比方 [1,2,3,15,3,4,5,15] s=14,,,仅仅有在15处将前面的元素又又一次加了一遍,故为2N

初始快慢指针都为0,fast指针向前移动。当slow和fast中连续字数组的和大于s时。我们就開始缩减窗体,不断的对slow进行向前移动直到sum小于s,然后再移动fast继续循环

代码:

class Solution {
public:
   // 法一
    /*int minSubArrayLen(int s, vector<int>& nums) {
        int result = nums.size();
        bool flag = false;
        for(int i = 0; i < nums.size(); i++){
            if(nums[i] >= s) return 1;
            int sum = nums[i];
            for(int j = i-1; j >= 0; j--){
                sum += nums[j];
                if(sum >= s){
                    result = min(result, i-j+1);
                    flag = true;
                    break;
                }
            }
        }
        if(flag)return result;
        return 0;
    }*/

    int minSubArrayLen(int s, vector<int>& nums) {     // 滑动窗体的形式+双指针
        int result = nums.size()+1;
        int frontPoint = 0, sum = 0;
        for(int i = 0; i < nums.size(); i++){
            sum += nums[i];
            while(sum >= s){    // 找到了窗体
                result = min(result, i - frontPoint + 1);   // 窗体是否满足要求
                sum -= nums[frontPoint++];            // 缩减窗体
            }
        }
        return result == (nums.size()+1) ?

0:result;
    }
};

二:Minimum
Window Substring

题目:

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).

For example,

S = "ADOBECODEBANC"

T = "ABC"

Minimum window is "BANC".

Note:

If there is no such window in S that covers all characters in T, return the emtpy string "".

If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.

分析:这道题刚開始我採用类似于上面滑动窗体的方法,可是每次都要推断当前字符串中是否全然包括字符串t,这个推断就会提升时间复杂度,结果导致TLE。后面參看了discuss中的方法,很巧妙,也放在这里。代码中map用来存放t字符串中的字符及出现的次数,而window用来存放字符串t中每一个字符在字符串s中出现的次数。lettercounts是一个标记变量,当等于t.size()的时候。就表示得到了一个全然包括t的字符子串。然后移动慢指针缩减窗体。

代码:

TLE:

class Solution {
public:
    bool isContain(const string &sstr, const string &t){
        for(int i = 0; i < t.size(); i++){
            if(sstr.find_first_of(t[i]) == string::npos) return false;
        }
        return true;
    }
    string minWindow(string s, string t) {
        int result = s.size()+1, frontPoint = 0;
        string str="";
        for(int i = 0; i < s.size(); i++){
            while(isContain(s.substr(frontPoint, i-frontPoint+1) , t)){
                if(result > i-frontPoint+1){
                    result = i-frontPoint+1;
                    str = s.substr(frontPoint, i-frontPoint+1);
                }
                frontPoint++;
            }
        }
        return str;
    }
};

AC代码:

class Solution {
public:

    string minWindow(string s, string t) {
        string result;
        if(s.size() == 0 || t.size() == 0) return result;
        unordered_map<char, int> map;
        unordered_map<char, int> window;  // 滑动窗体
        int lettercounts = 0;               // 标记变量,当等于t.size()的时候。该窗体就是一个全然包括字符串t的子串
        int minLen = s.size()+1;

        for(int i = 0; i < t.size(); i++)  // 将t放入map中。就是为了加速
            map[t[i]]++;

        for(int fast = 0, slow = 0; fast < s.size(); fast++){  // 快慢指针,快指针不断向前移动,
            char c = s[fast];
            if(map.find(c) != map.end()){      //
                window[c]++;
                if(window[c] <= map[c]){      // 变化lettercount变量
                    lettercounts ++;
                }
                if(lettercounts >= t.size()){           // 表示该窗体中已经所有包括t了
                    while(map.find(s[slow]) == map.end() || window[s[slow]] > map[s[slow]]){  // 对窗体进行缩减  1:slow所指向字符不在map中,2:在该子串中                                                                                               //出现非常多次  如BBABC    ABC slow指向B
                        window[s[slow]]--;
                        slow++;
                    }
                    if(minLen > fast - slow + 1){
                        minLen = fast - slow + 1;
                        result = s.substr(slow, minLen);
                    }
                    window[s[slow]]--;     // 缩减窗体
                    slow++;
                    lettercounts --;
                }

            }

        }
        return result;
    }
};

三:Contains
Duplicate III

题目:

Given an array of integers, find out whether there are two distinct indices i and j in
the array such that the difference between nums[i] and nums[j] is
at most t and
the difference between i and j is
at most k.

分析:这道题目也是滑动窗体,滑动窗体一般都是定义一个slow指针,然后一个fast指针不断向前滑动(循环遍历)。这个过程中我们要推断1:是否找到了窗体,2:窗体时否满足要求 3:窗体缩减等

此题也是,设置慢指针l,和快指针i遍历,窗体过大就缩减,判断找到的窗体是否满足要求,技巧是用到了关联容器的lower_bound函数,假设满足要求就返回true,否则返回false。

这里用set或者multiset都一样.。

。注意这里的auto是c++11的新特性。能够用来自己主动类型判断!

class Solution {
public:
    /*bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
        if(nums.size() < 2 || k == 0) return false;
        multiset<long> windows;       // 滑动窗体
        int l = 0;
        for(int i = 0; i < nums.size(); i++){
            if(i - l > k){     // 窗体大小超过了k 则须要删除nums[l]而且l++
                windows.erase(nums[l++]);
            }
            auto it = windows.lower_bound((long)nums[i] - (long)t);
            if(it != windows.end() && *it <= ((long)nums[i]+(long)t))    // 用long防止+1溢出
                return true;
            windows.insert(nums[i]);
        }
        return false;

    }*/

    bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
        if(nums.size() < 2 || k == 0) return false;
        set<long> windows;       // 滑动窗体
        int l = 0;               // 慢指针
        for(int i = 0; i < nums.size(); i++){
            if(i - l > k){     // 窗体大小超过了k 则须要删除nums[l]而且l++  窗体须要缩减了
                windows.erase(nums[l++]);
            }
            auto it = windows.lower_bound((long)nums[i] - (long)t);      //  即为nums[i]-t与nums[i]+t之间是否有元素
            if(it != windows.end() && *it <= ((long)nums[i]+(long)t))    // 用long防止+1溢出  找到了
                return true;
            windows.insert(nums[i]);             // not found
        }
        return false;

    }
};
时间: 2024-07-30 16:10:23

leetcode ---双指针+滑动窗体的相关文章

leetcode ---双指针+滑动窗口

一:Minimum Size Subarray Sum 题目: Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7, th

【剑指Offer学习】【面试题65:滑动窗体的最大值】

题目:给定一个数组和滑动窗体的大小,请找出全部滑动窗体里的最大值. 举例说明 比如,假设输入数组{2,3,4,2,6,2,5,1}及滑动窗体的大小.那么一共存在6个滑动窗体,它们的最大值分别为{4,4,6,6,6,5}. 解题思路 假设採用蛮力法,这个问题似乎不难解决:能够扫描每个滑动窗体的全部数字并找出当中的最大值.假设滑动窗体的大小为k,须要O(k)时间才干找出滑动窗体里的最大值.对于长度为n的输入数组,这个算法总的时间复杂度是O(nk). 实际上一个滑动窗体能够看成是一个队列.当窗体滑动时

Appium测试安卓Launcher以滑动窗体获得目标应用

所谓Launcher,指的是安卓的桌面管理程序,所有的应用图标都放在launcher上面.其实这是一个很简单的例子,只是为了验证几点想法而已. 1.实验目的 做这个试验的目的有二 尝试下窗体滑动函数swipe的使用 好奇究竟能不能正常的对安卓的Launcher进行指定package和activity进行测试 2.实验背景 过程是打算使用appium来启动launcher,然后滑动窗口去获取在第三个桌面的sdk自带应用"Notes".如下图所示 3. 试验步骤 3.1 获得launche

【转】Appium测试安卓Launcher以滑动窗体获得目标应用

原文地址:http://blog.csdn.net/zhubaitian/article/details/39755553 所谓Launcher,指的是安卓的桌面管理程序,所有的应用图标都放在launcher上面.其实这是一个很简单的例子,只是为了验证几点想法而已. 1.实验目的 做这个试验的目的有二 尝试下窗体滑动函数swipe的使用 好奇究竟能不能正常的对安卓的Launcher进行指定package和activity进行测试 2.实验背景 过程是打算使用appium来启动launcher,然

leetcode 239. 滑动窗口最大值(单调队列)

给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口内的 k 个数字.滑动窗口每次只向右移动一位. 返回滑动窗口中的最大值. 进阶: 你能在线性时间复杂度内解决此题吗? 示例: 输入: nums = [1,3,-1,-3,5,3,6,7], 和 k = 3输出: [3,3,5,5,6,7] 解释: 滑动窗口的位置 最大值--------------- -----[1 3 -1] -3 5 3 6 7 3 1 [3 -1 -3] 5 3 6

Leetcode 480.滑动窗口中位数

滑动窗口中位数 中位数是有序序列最中间的那个数.如果序列的大小是偶数,则没有最中间的数:此时中位数是最中间的两个数的平均数. 例如: [2,3,4],中位数是 3 [2,3],中位数是 (2 + 3) / 2 = 2.5 给出一个数组 nums,有一个大小为 k 的窗口从最左端滑动到最右端.窗口中有 k 个数,每次窗口移动 1 位.你的任务是找出每次窗口移动后得到的新窗口中元素的中位数,并输出由它们组成的数组. 例如: 给出 nums = [1,3,-1,-3,5,3,6,7],以及 k = 3

【leetcode 239. 滑动窗口最大值】解题报告

思路:滑动窗口的思想,只要是求连续子序列或者子串问题,都可用滑动窗口的思想 方法一: vector<int> maxSlidingWindow(vector<int>& nums, int k) { vector<int> res; if (nums.size()==0) return res; int i=0; deque<int> dq; for (i=0;i<nums.size();++i) { while(!dq.empty()&

Leetcode 239. 滑动窗口最大值

class Solution { public: vector<int> maxSlidingWindow(vector<int>& nums, int k) { vector<int> ret; if(nums.size()==0) return ret; deque<int> q; for(int i=0; i<nums.size(); ++i) { if(q.empty() || nums[q.back()] > nums[i] )

&lt;LeetCode&gt;双指针题目&#183;

1.82. Remove Duplicates from Sorted List II (Medium) Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Example 1: Input: 1->2->3->3->4->4->5 Output: 1->2-&g