Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
master
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
The license selected for the repository is subject to the license used by the main branch of the repository.
master
Branches (1)
master
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
master
algorithm
/
src
/
arrays
/
array_24.java
algorithm
/
src
/
arrays
/
array_24.java
array_24.java 1.83 KB
Copy Edit Raw Blame History
Byte authored 2023年07月09日 20:13 +08:00 . 算法review
package arrays;
import java.util.HashMap;
import java.util.Map;
/**
* 题目:
* 给定一个数组arr,返回arr的最长无重复子数组的长度(无重复指的是所有数字都不相同)。
* @Author Gavin
* @date 2022年08月06日 17:27
*/
public class array_24 {
/**
解题思路:
* 用一个map来存key,需要把数组下标记录下来,需要注意的有两点,一个就是如果找到一个重复元素了那么外层下标就可以从
* 第一个重复元素重新开始计算,这样可以大大提升性能,还有如果内层循环到末尾了就可以直接返回,因为已经是最大的了
*
* 与动态规划中求最长无重复字符串长度的思路一样
*/
public static int solution(int[] arr) {
if(arr==null) return 0;
if(arr.length==1) return 1;
int max = 0;
Map<Integer,Integer> map = new HashMap<>();
int leftIndex = 0;
int len = arr.length;
while(leftIndex<len){
int repeatData = -1;
for(int i=leftIndex;i<len;i++){
if(map.containsKey(arr[i])){
repeatData = arr[i];
break;
}
map.put(arr[i],i);
}
if(repeatData==-1){
//说明没有遇到重复数据,一直到尾巴,这时就已经产生最大值了,
//没必要继续了
return Math.max(max,map.size());
}else{
//遇到了重复数据,但还没到尾巴,最大值不确定,需要继续
//左界限指针直接跳到重复元素(第1个)的后面
leftIndex = map.get(repeatData)+1;
max = Math.max(max,map.size());
map.clear();
}
}
return max;
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

Releases

No release

Contributors

All

Language(Optional)

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Gavery/algorithm.git
git@gitee.com:Gavery/algorithm.git
Gavery
algorithm
algorithm
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /