[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 ...
随机推荐
- ubuntu18下lamp虚拟路劲配置
一.配置二级域名 修改hosts文件,模拟dns解析. 位置:/etc/hosts 添加 127.0.0.1 myweb.service.com 二.创建项目目录 apache默认目录是/var m ...
- Python_2day
选择 布尔类型.数值和表达式 注意:比较运算符的相等是两个等号,一个等到代表赋值 在Python中可以用整型0来代表False,其他数字来代表True 后面还会讲到 is 在判断语句中的用发 In [ ...
- WTF!! Vue数组splice方法无法正常工作
当函数执行到this.agents.splice()时,我设置了断点.发现传参index是0,但是页面上的列表项对应的第一行数据没有被删除, WTF!!! 这是什么鬼!然后我打开Vue Devtool ...
- sqlserver2008 批量导出所有的作业
SQl server 代理 -- 选中作业 -- 按 F7,弹出 对象资源管理详细信息 ,里面对作业多选以后右键就有导出 sql的菜单了
- export ,export default 和 import 区别以及用法
首先要知道export,import ,export default是什么 ES6模块主要有两个功能:export和importexport用于对外输出本模块(一个文件可以理解为一个模块)变量的接口i ...
- Git生成公钥.pub 及秘钥 命令
Git生成公钥.pub 及秘钥 命令 ssh-keygen -t rsa -C "******@qq.com" 将.pub公钥里面内容复制到github或者将这文件交给git管理员 ...
- docker快速入门01——docker安装与简单应用
1.docker简介 Docker 是一个开源的应用容器引擎,Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化 ...
- Win10+Linux(CentOS) 双系统安装教程--踩坑实录
最近心血来潮想给自己的笔记本装一套linux系统作为开发环境, 说干就干,首先先收集一下现在linux主流版本, 貌似现在市场上应用服务器比较多的是redhat相关产品,而ubuntu的优势在于它庞大 ...
- PAT Advanced 1041 Be Unique (20 分)
Being unique is so important to people on Mars that even their lottery is designed in a unique way. ...
- #1055 ... sql_mode=only_full_group_by
sql_mode=only_full_group_by 版权声明:本文为参考多篇博主文章,略作测试修改. 参考文章: 猿医生 的<5分钟学会MySQL-this is incompatible ...