[LeetCode]-algorithms-Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example,
the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3.
For "bbbbb" the longest substring is "b", with the length of 1.
分析:求最大不重复子串的长度
public int lengthOfLongestSubstring(String s) {
char[] arr = s.toCharArray();
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
int len = 0;
for(int i=0; i<arr.length; i++){
if(map.containsKey(arr[i])){
len = Math.max(len, map.size());
i = map.get(arr[i]);
map.clear();
}else{
map.put(arr[i],i);
}
}
len = Math.max(len, map.size());
return len;
}
结语:依次把字符串的每个字符以及它对应的下表索引存入到Map中,字符为键,下表索引为值
每插入一次,进行判断,如果存在则从这个重复的字符的下一个开始遍历
[LeetCode]-algorithms-Longest Substring Without Repeating Characters的更多相关文章
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- [LeetCode][Python]Longest Substring Without Repeating Characters
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- LeetCode之Longest Substring Without Repeating Characters
[题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...
- Leetcode 3. Longest Substring Without Repeating Characters (Medium)
Description Given a string, find the length of the longest substring without repeating characters. E ...
- [Leetcode Week1]Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
随机推荐
- Node.js FS模块方法速查
1. File System 所有文件操作提供同步和异步的两种方式,本笔记只记录异步的API 异步方式其最后一个参数是回调函数.回调函数的第一个参数往往是错误对象,如果没有发生参数,那么第一个参数可能 ...
- js变量的作用域与函数作用域
引自 1. 变量的作用域(var与let的区别) 在函数之外声明的变量,叫做全局变量,因为它可被当前文档中的任何其他代码所访问.在函数内部声明的变量,叫做局部变量,因为它只能在当前函数的内部访问. E ...
- Css布局 响应式布局介绍
1. 概念: 写一套css样式可以同时适配多个终端,是为解决移动互联网诞生的. 2. 作用: 面对不同的分辨率设备灵活性强,能够快捷解决多设备显示适应问题 3. 原理 媒体查询 ① 外联式媒体查询语法 ...
- leetcode34. 在排序数组中查找元素的第一个和最后一个位置
给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在目标值,返回 [ ...
- iptables - IP包过滤器管理
总览 iptables -ADC 指定链的规则 [-A 添加 -D 删除 -C 修改] iptables - RI iptables -D chain rule num[option] iptable ...
- 6U VPX 加固智能计算异构服务器
6U VPX 加固智能计算异构服务器 北京太速科技有限公司在线客服:QQ:448468544 公司网站:www.orihard.com联系电话:15084122580
- 牛客练习赛26 D xor序列 (线性基)
链接:https://ac.nowcoder.com/acm/contest/180/D 来源:牛客网 xor序列 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他 ...
- Django学习系列3:创建仓库
在创建仓库之前,在项目superlists中新建一个Python文件,命名为functional_tests.py,里面的内容如下: # File: functional_test.py # Auth ...
- AI换脸教程:DeepFaceLab使用教程(2.训练及合成)
如果前期工作已经准备完毕(DeepFaceLab下载(https://www.deepfacelabs.com/list-5-1.html),然后安装相应的显卡驱动,DeepFaceLab使用教程(1 ...
- java集合源码分析几篇文章
java集合源码解析https://blog.csdn.net/ns_code/article/category/2362915