leetcode76 904
leetcode76 & 904
题目:水果成篮
class Solution {
public:int totalFruit(vector<int>& fruits) {if (fruits.size() < 3){return fruits.size();}int n1 = fruits[0];int x = 1;while (x < fruits.size() && fruits[x] == n1){x++;}if (x == fruits.size()){return fruits.size();}# 对特殊情况进行单独分类讨论int n2 = fruits[x];int l = x + 1;int s = 0;int temp;for (int i = x + 1; i < fruits.size(); i++){if (fruits[i] != n1 && fruits[i] != n2){s = i - 1;while (fruits[s] == fruits[i - 1]){s--;}s++;n1 = fruits[i];n2 = fruits[i - 1]; }l = l > (i - s + 1) ? l : (i - s + 1);}return l;}
};class Solution {
public:int totalFruit(vector<int>& fruits) {int n = fruits.size();unordered_map<int, int> cnt;# 要学会使用STL作为工具int left = 0, ans = 0;for (int right = 0; right < n; ++right) {++cnt[fruits[right]];while (cnt.size() > 2) {auto it = cnt.find(fruits[left]);--it->second;if (it->second == 0) {cnt.erase(it);}++left;}# 采用了不同的方法寻找左边界ans = max(ans, right - left + 1);}return ans;}
};
题目:最小覆盖字串
class Solution {
public:unordered_map<char, int> test, c;bool compare() {for (auto i : test) {if (i.second > c[i.first]) {return false;}}return true;}#这样写可以加速程序运行string minWindow(string s, string t) {for (auto i : t) {test[i]++;}int fs = 0;int begin = 0;int num = s.size() + 1;for (int i = 0; i < s.size(); i++) {c[s[i]]++;while (compare() && begin <= i) {if (i - begin + 1 < num) {num = i - begin + 1;fs = begin;}c[s[begin++]]--;}}string r = (num == s.size() + 1) ? "" : s.substr(fs, num);return r;}
};#滑动数组基本思想,右边界点移至满足条件后,移动左边界点 class Solution {
public:string minWindow(string s, string t) {vector<int> need(128, 0);int count = 0;for (char c : t){need[c]++;}count = t.length();int l = 0, r = 0, start = 0, size = INT_MAX;while (r < s.length()){char c = s[r];if (need[c] > 0)count--;need[c]--; //先把右边的字符加入窗口if (count == 0) //窗口中已经包含所需的全部字符{while (l < r && need[s[l]] < 0) //缩减窗口{need[s[l++]]++;} //此时窗口符合要求if (r - l + 1 < size) //更新答案{size = r - l + 1;start = l;}need[s[l]]++; //左边界右移之前需要释放need[s[l]]l++;count++;}r++;}return size == INT_MAX ? "" : s.substr(start, size);}
};
#该法和最开始的的方法类似,但该法利用need[s[l]]<0可以将左侧多余的点全部清除
leetcode76 904相关推荐
- oracle 904 maxsize,EXP-00008: 遇到 ORACLE 错误 904 ORA-01003
今日导数据遇到一错误,记得之前也遇到过,但是没做记录,在此记一记,便于加深印象. 使用exp导数据的时候,在本地连远程DB服务器执行如下语句:exp **/**@192.168.90.125/cent ...
- oracle错误 904,ORACLE 导出错误 EXP-00008: 遇到 Oracle 错误 904
案例情景--在一次 C:\Documents and Settings\Administrator>explsxy/lsxy@lsxy_dbfile=E:\lsxy.dmp owner=lsxy ...
- mysql报904_EXP-00008:遇到ORACLE错误904问题
案例情景--在一次Oracle 数据库导出时: C:\Documents and Settings\Administrator>exp lsxy/lsxy@lsxy_db file=E:\lsx ...
- leetcode76. 最小覆盖子串
leetcode76. 最小覆盖子串 题目描述 链接: leetcode76. 给你一个字符串 s .一个字符串 t .返回 s 中涵盖 t 所有字符的最小子串.如果 s 中不存在涵盖 t 所有字符的 ...
- 904. 水果成篮(数组、滑动窗口)
904. 水果成篮(数组.滑动窗口) 考察点: 数组.滑动窗口 水果成篮 你正在探访一家农场,农场从左到右种植了一排果树.这些树用一个整数数组 fruits 表示,其中 fruits[i] 是第 i ...
- C++:Leetcode-滑动窗口-904.水果成篮
C++:Leetcode-滑动窗口-904水果成篮 重点掌握滑动窗口思想,左指针和右指针分别代表的含义 重点掌握熟悉利用哈希表进行统计和查重. 面对这种同类型数量统计问题或者是查重问题,哈希表是不二之 ...
- LeetCode 904. 水果成篮
904. 水果成篮 题目:你正在探访一家农场,农场从左到右种植了一排果树.这些树用一个整数数组 fruits 表示,其中 fruits[i] 是第 i 棵树上的水果 种类 . 你想要尽可能多地收集水果 ...
- LeetCode每日一题——904. 水果成篮
LeetCode每日一题系列 题目:904. 水果成篮 难度:普通 文章目录 LeetCode每日一题系列 题目 示例 思路 题解 题目 你正在探访一家农场,农场从左到右种植了一排果树.这些树用一个整 ...
- 【042】904. 水果成篮[滑动窗口]
你正在探访一家农场,农场从左到右种植了一排果树.这些树用一个整数数组 fruits 表示,其中 fruits[i] 是第 i 棵树上的水果 种类 . 你想要尽可能多地收集水果.然而,农场的主人设定了一 ...
最新文章
- webpack自动打包功能配置
- 计算机网络共享是什么意思,网络共享与便携式热点是什么意思
- java执行时的两个常见问题(无法加载主类)
- 2019-10-06 线性系统的理解
- ABAP web service运行时的细节调试
- SpringBoot 配置文件bootstrap和application的区别
- cocos2d 很水很水的看法
- Java面试题:JDK不同版本处理IO流异常的标准代码
- 浅谈!important对CSS的重要性
- 深入理解JavaScript定时器
- Redis 更新(set) key值 会重置过期时间问题
- 数据分享 | 高光谱遥感影像数据集汇总
- java中的Map每次只能put一次,写段增强的put,可以一次put很多次
- baacloud是正规软件吗_baacloud,
- 计算机无法识别外接光驱,USB外置光驱不能用怎么办 USB外置光驱无法识别解决方法...
- 简单聊聊利用DNS服务器提供单一入口
- 基于线性回归预测环境空气质量 代码+数据
- 【ELM】动态自适应可变加权极限学习机ELM预测(Matlab代码实现)
- 【详细】Python基础(一)
- 用Python对案例进行Ridge算法和Lasso算法的比较