【C/C++】最长无重复子数组
题目描述
给定一个数组arr,返回arr的最长无重复元素子数组的长度,无重复指的是所有数字都不相同。
子数组是连续的,比如[1,2,3,4,5]的子数组有[1,2],[2,3,4]等等,但是[1,3,4]不是子数组
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int maxLength(vector<int> & arr)
{
int maxlen = 0;
set<int> st;
int i = 0, j = 0;
while( j < arr.size())
{
while(st.count(arr[j]))
{
st.erase(arr[i++]);
}
st.insert(arr[j++]);
maxlen = max(maxlen, j - i);
}
return maxlen;
}
};
int main()
{
vector <int> arr = {2,2,3,4,3};
Solution solution;
cout << solution.maxLength(arr) << endl;
system("pause");
}
【C/C++】最长无重复子数组的更多相关文章
- [LeetCode] Maximum Length of Repeated Subarray 最长的重复子数组
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...
- [LeetCode] 718. Maximum Length of Repeated Subarray 最长的重复子数组
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...
- LeetCode第三题—— Longest Substring Without Repeating Characters(最长无重复子字符串)
题目描述 Given a string, find the length of the longest substring without repeating characters. Example ...
- LeetCode 718. 最长重复子数组(Maximum Length of Repeated Subarray)
718. 最长重复子数组 718. Maximum Length of Repeated Subarray 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s,找出该数组中满足其和 ≥ s 的 ...
- Java实现 LeetCode 718 最长重复子数组(动态规划)
718. 最长重复子数组 给两个整数数组 A 和 B ,返回两个数组中公共的.长度最长的子数组的长度. 示例 1: 输入: A: [1,2,3,2,1] B: [3,2,1,4,7] 输出: 3 解释 ...
- 【Leetcode】718. 最长重复子数组
最长重复子数组有一下性质 A: [1,2,3,2,1] B: [3,2,1,4,7]设横是A竖是B,有规律:若横元和竖元相等,则为1,不等为0 1 2 3 2 13 0 0 1 0 12 0 1 0 ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- lintcode: 最长无重复字符的子串
题目 最长无重复字符的子串给定一个字符串,请找出其中无重复字符的最长子字符串. 例如,在"abcabcbb"中,其无重复字符的最长子字符串是"abc",其长度为 ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
随机推荐
- 菜鸡的Java笔记 第三十二 - java 静态导入的实现
静态导入的实现 为了理解静态导入的操作产生的动机,下面通过一个具体的代码来观察 范例:现在有一个 Import 的类,这个类中的方法全部都是 static 方法 packa ...
- [luogu7418]Counting Graphs P
参考[luogu7417],同样求出最短路,得到二元组$(x,y)$并排序,记$tot_{(x,y)}$为$(x,y)$的数量 其中所给的两个条件,即分别要求: 1.$(x,y)$只能和$(x\pm ...
- [hdu7020]Array
(这是一个线性的做法) 显然对于合法的区间,众数是唯一的,因此不妨枚举众数,将众数标记为1.其余数标记为-1,此时问题即求有多少个区间和大于0 考虑暴力的做法:从左到右枚举右端点,记当前前缀和为$to ...
- vue-if和show
<template> <div> <div v-if="flag">今晚要上课</div> <div v-else> 今 ...
- NOI 2008 志愿者招募
NOI 2008 志愿者招募 考虑用 $ p_i $ 表示第 $ i $ 天实际招收的人数,我们假设我们有三种志愿者,分别是 $ 1\to 2,1 \to 3 , 2\to 3 $ ,我们招手的人数分 ...
- 2基因组间鉴定SV
本文学习费章军老师文章Genome of Solanum pimpinellifolium provides insights into structural variants during toma ...
- Hive-删除表(drop、truncate的区别)
Hive删除操作主要分为几大类:删除数据(保留表).删除库表.删除分区.我将以下图为例清空iot_devicelocation中的数据,之后再删除表.库等. 解释: use xpu123; #使用 ...
- C++栈溢出
先看一段代码 #include<iostream> using namespace std; #define n 510 void sum(int a,int b) { cout<& ...
- 修改linux系统下mysql数据库登陆密码(密码忘记)
报错:Access denied for user 'root'@'localhost' (using password: NO) 解决方案: 1. 检查mysql服务是否启动,如果启动,关闭mysq ...
- Java日期格式转换不用发愁
前言 Java 中日期.时间相关的类相当的多,并且分不同的版本提供了不同的实现,包括 Date . Calendar . LocalDateTime . ZoneDateTime . OffsetDa ...