滑动窗口算法思想,找出字符串中的所有字母异位词

力扣438滑动窗口

在这里插入图片描述
在这里插入图片描述
力扣大神写的诗
一开始不理解滑动窗口是什么!
看完此诗我才明白,滑动窗口其实就是双指针的一种形式
左右指针齐头进。

c#代码如下

public class Solution {
    public IList<int> FindAnagrams(string s, string p) {
   Dictionary<char, int> need = new Dictionary<char, int>();
            Dictionary<char, int> window = new Dictionary<char, int>();
            for (int i = 0; i < p.Length; i++)
            {
                if (need.ContainsKey(p[i]))
                    need[p[i]]++;
                else
                    need.Add(p[i], 1);
            }
            int left = 0;
            int right = 0;
            int valid = 0;
            IList<int> res = new List<int>();
            while (right < s.Length)
            {
                char c = s[right];
                right++;
                if (need.ContainsKey(c))
                {
                    if (window.ContainsKey(c))
                        window[c]++;
                    else
                        window.Add(c, 1);
                    if (window[c] == need[c])
                        valid++;
                }
                while (right - left >= p.Length)
                {
                    if (valid == need.Count)
                    {
                        res.Add(left);
                    }
                    char d = s[left];
                    left++;
                    if (need.ContainsKey(d))
                    {
                        if (window[d] == need[d])
                            valid--;
                        window[d]--;
                    }
                }
            }
            return res;
    }
}

在这里插入图片描述

c#代码 用双指针解决上面问题

public class Solution {
    public bool BackspaceCompare(string s, string t) {
           int sRight = s.Length - 1;
            int tRight = t.Length - 1;
            int skipS = 0;
            int skipT = 0;
            while (sRight >= 0 || tRight >= 0)
            {
                while (sRight >= 0)
                {
                    if (s[sRight] == '#')
                    {
                        skipS++;
                        sRight--;
                    }
                    else if (skipS > 0)
                    {
                        skipS--;
                        sRight--;
                    }
                    else
                        break;
                }
                while (tRight >= 0)
                {
                    if (t[tRight] == '#')
                    {
                        skipT++;
                        tRight--;
                    }
                    else if (skipT > 0)
                    {
                        skipT--;
                        tRight--;
                    }
                    else
                        break;
                }
                if (sRight >= 0 && tRight >= 0)
                {
                    if (s[sRight] != t[tRight])
                        return false;
                }
                else if (sRight >= 0 || tRight >= 0)
                    return false;
                sRight--;
                tRight--;
            }
            return true;
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值