力扣刷题 day7

文章目录

水果成篮


904. 水果成篮 - 力扣(LeetCode)

在这里插入图片描述


暴力枚举 :


    // 暴力 + 哈希 进行枚举
    public static int totalFruit2(int[] fruits) {
        int maxCount = 0;
        for (int i = 0; i < fruits.length; i++) {
            HashMap<Integer, Integer> map = new HashMap<>();
            int j = i;
            for (; j < fruits.length; j++) {
                Integer nowNumber = map.get(fruits[j]);
                if (map.size() < 2 || nowNumber != null) {
                    if (nowNumber != null) {
                        map.put(fruits[j], nowNumber + 1);
                    } else {
                        map.put(fruits[j], 1);
                    }
                } else {
                    // 此时遇到第三个元素了
                    maxCount = Math.max(j - i, maxCount);
                    break;
                }
            }
            if (j == fruits.length) {
                maxCount = Math.max(j - i, maxCount);
            }
        }
        return maxCount;
    }

在这里插入图片描述

滑动窗口:

在这里插入图片描述


使用 HashMap:

class Solution {
    public int totalFruit(int[] fruits) {
        int left = 0;
        int right = 0;
        int maxCount = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        while (right < fruits.length) {
            int nowNumber = fruits[right];
            // 进窗口
            map.put(nowNumber, map.getOrDefault(nowNumber, 0) + 1);

            // 出窗口
            while (map.size() >= 3) {
                Integer leftNowNumber = map.get(fruits[left]);
                if (leftNowNumber != null) {
                    if (leftNowNumber == 1) {
                        map.remove(fruits[left]);
                    } else {
                        map.put(fruits[left], leftNowNumber - 1);
                    }
                }
                left++;
            }
            // 更新结果
            maxCount = Math.max(right - left + 1, maxCount);
            right++;
        }
        return maxCount;
    }
}

使用数组模拟 HashMap

class Solution {
    public int totalFruit(int[] fruits) {
        int[] hash = new int[fruits.length + 1];
        int left = 0;
        int right = 0;
        int nowCount = 0;
        int maxCount = 0;
        while (right < fruits.length) {
            // 进窗口
            if (hash[fruits[right]] == 0) {
                nowCount++;
            }
            hash[fruits[right]]++;
            // 判断
            while (nowCount > 2) {
                // 出窗口
                if (hash[fruits[left]]-- == 1) {
                    nowCount--;
                }
                left++;
            }
            // 更新结果
            maxCount = Math.max(maxCount, right - left + 1);
            right++;
        }
        return maxCount;
    }
}


python:

class Solution(object):
    def totalFruit(self, fruits):
        left, right, maxCount, nowCount = 0, 0, 0, 0
        hash = [0] * (len(fruits) + 1)
        while right < len(fruits):
            if hash[fruits[right]] == 0:
                nowCount += 1
            hash[fruits[right]] += 1
            while nowCount > 2:
                if hash[fruits[left]] == 1:
                    nowCount -= 1
                hash[fruits[left]] -= 1
                left += 1
            maxCount = max(right - left + 1, maxCount)
            right += 1
        return maxCount

异位词


438. 找到字符串中所有字母异位词 - 力扣(LeetCode)

在这里插入图片描述

暴力枚举:


    // 异位词
    public static List<Integer> findAnagrams2(String s, String p) {

        int[] cur2 = new int[26];
        // 记录 p 
        for (int i = 0; i < p.length(); i++) {
            cur2[p.charAt(i) - 'a']++;
        }
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < s.length(); i++) {
            int[] cur1 = new int[26];
            boolean sTag = true;
            for (int j = i; j < i + p.length() && j < s.length(); j++) {
                cur1[s.charAt(j) - 'a']++;
            }
            // 此时找到了一组进行判断
            for (int z = 0; z < 26; z++) {
                if (cur1[z] != cur2[z]) {
                    // 使用 sTag 来表示 找到的 字串是否为 p 的异位词
                    sTag = false;
                    break;
                }
            }
            if (sTag) {
                // 记录下当前位置
                list.add(i);
            }

        }
        return list;
    }

在这里插入图片描述

固定长度滑动窗口

class Solution {
    // 固定长度滑动窗口
    public List<Integer> findAnagrams(String s, String p) {
        int left = 0;
        int right = 0;
        List<Integer> response = new ArrayList<>();
        int[] cur1 = new int[26];
        for (char a : p.toCharArray()) {
            cur1[a - 'a']++;
        }
        int[] cur2 = new int[26];
        while (right < s.length()) {
            // 进窗口
            cur2[s.charAt(right) - 'a']++;
            if (right - left + 1 > p.length()) {
                // 此时超出-->出窗口
                cur2[s.charAt(left++) - 'a']--;
            }

            // 校验当前 窗口内的元素是否为 p 的异位词
            if (right - left + 1 == p.length()) {
                if (check(cur1, cur2)) {
                    response.add(left);
                }
            }
            right++;
        }

        return response;
    }

    public boolean check(int[] cur1, int[] cur2) {
        for (int i = 0; i < 26; i++) {
            if (cur1[i] != cur2[i]) {
                return false;
            }
        }
        return true;
    }
}

在这里插入图片描述

优化:


    // 优化 --> 不循环 26 次进行比较遍历
    public static List<Integer> findAnagrams(String s, String p) {
        int left = 0;
        int right = 0;
        List<Integer> response = new ArrayList<>();
        int[] cur1 = new int[26];
        for (char a : p.toCharArray()) {
            cur1[a - 'a']++;
        }
        int[] cur2 = new int[26];
        int count = 0;
        while (right < s.length()) {
            // 进窗口 --> cur1 记录 p  cur2 记录 s
            cur2[s.charAt(right) - 'a']++;
            if (cur2[s.charAt(right) - 'a'] <= cur1[s.charAt(right) - 'a']) {
                // 此时满足情况
                count++;
            }
            // 出窗口
            if (right - left + 1 > p.length()) {
                // 判断是否出掉有效字符
                if (cur1[s.charAt(left) - 'a'] >= cur2[s.charAt(left) - 'a']) {
                    count--;
                }
                cur2[s.charAt(left) - 'a']--;
                left++;
            }
            // 更新结果
            if (count == p.length()) {
                response.add(left);
            }
            right++;
        }
        return response;
    }

python:


class Solution(object):
    def findAnagrams(self, s, p):
        left, right, count = 0, 0, 0
        response = []
        cur1, cur2 = [0] * 26, [0] * 26
        for a in p:
            cur1[ord(a) - 97] += 1
        while right < len(s):
            # 进窗口
            cur2[ord(s[right]) - 97] += 1
            if cur2[ord(s[right]) - 97] <= cur1[ord(s[right]) - 97]:
                count += 1
            # 判断 + 出窗口
            if right - left + 1 > len(p):
                if cur2[ord(s[left]) - 97] <= cur1[ord(s[left]) - 97]:
                    count -= 1
                cur2[ord(s[left]) - 97] -= 1
                left += 1
            # 更新结果
            if count == len(p):
                response.append(left)
            right += 1
        return response

好的,关于力扣刷题C++常用操作,我可以给你一些常见的操作和技巧: 1. 使用 STL 容器和算法库:STL(Standard Template Library)是 C++ 标准库中的一个重要组成部分,包含了许多常用的容器和算法。在力扣刷题中,使用 STL 可以大大提高代码的效率和可读性。例如,vector 可以用来存储动态数组,sort 可以用来排序等等。 2. 使用 auto 关键字:auto 关键字可以自动推导变量类型,可以减少代码量和提高可读性。例如,auto x = 1; 可以自动推导出 x 的类型为 int。 3. 使用 lambda 表达式:lambda 表达式是 C++11 中引入的一种匿名函数,可以方便地定义一些简单的函数对象。在力扣刷题中,使用 lambda 表达式可以简化代码,例如在 sort 函数中自定义比较函数。 4. 使用位运算:位运算是一种高效的运算方式,在力扣刷题中经常会用到。例如,左移运算符 << 可以用来计算 2 的幂次方,右移运算符 >> 可以用来除以 2 等等。 5. 使用递归:递归是一种常见的算法思想,在力扣刷题中也经常会用到。例如,二叉树的遍历、链表的反转等等。 6. 使用 STL 中的 priority_queue:priority_queue 是 STL 中的一个容器,可以用来实现堆。在力扣刷题中,使用 priority_queue 可以方便地实现一些需要维护最大值或最小值的算法。 7. 使用 STL 中的 unordered_map:unordered_map 是 STL 中的一个容器,可以用来实现哈希表。在力扣刷题中,使用 unordered_map 可以方便地实现一些需要快速查找和插入的算法。 8. 使用 STL 中的 string:string 是 STL 中的一个容器,可以用来存储字符串。在力扣刷题中,使用 string 可以方便地处理字符串相关的问题。 9. 注意边界条件:在力扣刷题中,边界条件往往是解决问题的关键。需要仔细分析题目,考虑各种边界情况,避免出现错误。 10. 注意时间复杂度:在力扣刷题中,时间复杂度往往是评判代码优劣的重要指标。需要仔细分析算法的时间复杂度,并尽可能优化代码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值