zoukankan      html  css  js  c++  java
  • 904. 水果成篮

    题目:https://leetcode-cn.com/problems/fruit-into-baskets/

    主要使用滑动窗口方法

    我的答案(参考“胖球”的题解:https://leetcode-cn.com/u/chun-hua-qiu-shi-2/)

    C++版本:
    int totalFruit(vector<int>& fruits) {
        if (fruits.size() == 0) return 0;
        int result = -1;
        int sumTree = 0;
        int i = 0;
        int Basket1 = 0;//第一个篮子
        int Basket2 = 0;//第二个篮子
        for (int j = 0; j < fruits.size(); j++){//j用来遍历整个数组
            if(fruits[j] != fruits[Basket1] && fruits[j] != fruits[Basket2]){
                if (Basket1 == Basket2) {
                    Basket2 = j;
                }
                else{
                    Basket1 = i;
                    Basket2 = j;
                }
            }
            sumTree = j - Basket1 + 1;
            result = sumTree > result ? sumTree : result;
            if (fruits[i] != fruits[j]){
                i = j;
            }
        }
        return  result == -1 ? 0 : result;
    }

    胖球的思路:

    思路:
        类似于滑动窗口的多指针解法
        我们需要在遍历tree,索引为i时,不断更新以下三个指针:
        当下的两个篮子第一个篮子起始索引first,
        当下的两个篮子第二个篮子起始索引second,
        未来的两个篮子第一个篮子的起始索引temp,
        计算两个篮子中i - first + 1的最大值即可求出水果最大长度len
    如何计算first:
        遍历tree记录第一个出现的篮子的索引,
        当遍历到出现第三个篮子时(tree[i] != tree[first] && tree[i] != tree[second])将其更新为temp,
        first = temp,保证计算len时同时出现的只有两个篮子
    如何计算second:
        遍历tree记录第二个出现的篮子的索引,
        当遍历到出现第三个篮子时(tree[i] != tree[first] && tree[i] != tree[second])将其更新为i,
        second = i,保证计算len时同时出现的只有两个篮子
    如何计算temp:
        以010123为例,
        遍历完索引为3(值为1)时(只有两个篮子),first = 0,second = 1,temp = 3。
        遍历完索引为4(值为2)时(出现三个篮子),first = 3,second = 4,temp = 4。
        以010023为例,
        遍历完索引为3(值为0)时(只有两个篮子),first = 0,second = 1,temp = 2。
        遍历完索引为4(值为2)时(出现三个篮子),first = 2,second = 4,temp = 4。
        有如下规律:
        当下两个篮子中最后出现的篮子从后往前连续相等最长的索引,即为未来的两个篮子中第一个篮子的起始索引
        0101最后出现的篮子是1索引是3,和1从后往前连续相等最长的索引还是3
        0100最后出现的篮子时0索引是3,和0从后往前连续相等最长的索引是2
    
    class Solution {
        public int totalFruit(int[] tree) {
            if (tree.length == 0) {
                return 0;
            }
            int len = 0,first = 0,second = 0,temp = 0;
            for (int i = 0; i < tree.length; i++) {
                //判断是否出现第三个篮子
                if (tree[i] != tree[first] && tree[i] != tree[second]){
                    //只有初始化时,第一个篮子和第二个篮子都为0时才相等
                    //相等时出现的是第二个篮子,故不用更新first,只需要更新second
                    //不相等时出现的是第三个篮子,故first,second都需要更新
                    if(first != second) {
                        first = temp;
                    }
                    second = i;
                }
                //更新len最大值
                len = Math.max(len,i - first + 1);
                //计算temp
                if(tree[temp] != tree[i]){
                    temp = i;
                }
            }
            return len;
        }
    }
  • 相关阅读:
    javascript、jQuery的扩展方法,扩展实例展示代码
    经典弹出层Colorbox
    在Entity Framework中使用事务
    WCF中常用的binding方式
    前端上传组件Plupload使用指南
    SQL ROW_NUMBER() OVER函数的基本用法用法
    数据库选项--触发器
    数据库选项--会刷新过程缓存的选项
    数据库选项--页面检查
    数据库选项--自动更新统计
  • 原文地址:https://www.cnblogs.com/wltree/p/15405035.html
Copyright © 2011-2022 走看看