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的更多相关文章

  1. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...

  2. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  3. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  4. [LeetCode][Python]Longest Substring Without Repeating Characters

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  5. LeetCode之Longest Substring Without Repeating Characters

    [题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...

  6. Leetcode 3. Longest Substring Without Repeating Characters (Medium)

    Description Given a string, find the length of the longest substring without repeating characters. E ...

  7. [Leetcode Week1]Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...

  8. [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  9. LeetCode[3] Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...

  10. 【leetcode】Longest Substring Without Repeating Characters

    题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...

随机推荐

  1. 03: redis高级

    1.1 布隆过滤器 1.布隆过滤器是什么?(判断某个key一定不存在) 1. 本质上布隆过滤器是一种数据结构,比较巧妙的概率型数据结构 2. 特点是高效地插入和查询,可以用来告诉你 “某样东西一定不存 ...

  2. 使用elasticsearch7.3版本在一台主机上部署多个实例组建集群

    系统:centos 7.4 x64 主机ip:192.168.0.160 软件包:elasticsearch-7.3.0-linux-x86_64.tar.gz 配置步骤 vim /etc/secur ...

  3. Asp.net Core中文转换成拼音

    一.概述 之前使用.net framework,可以使用Microsoft Visual Studio International Feature Pack 1.0 进行转换,现在使用asp.net ...

  4. sql server ABS函数和PI函数

    --ABS(x)返回x的绝对值 --PI()返回圆周率的值

  5. Directed Roads CodeForces - 711D (基环外向树 )

    ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it co ...

  6. TensorFlow for python学习使用

    一.TensorFlow简介 TensorFlow 是由 Google Brain 团队为深度神经网络(DNN)开发的功能强大的开源软件库.当前流行的深度学习框架,从中能够清楚地看到 TensorFl ...

  7. 如何在Linux下安装Tomcat

    上篇文章写到了Linux下安装JDK1.8,这篇文章详细阐述一下 如何在Linux下安装Tomcat!!!有啥问题可以留言,博主每天都会看博客的. 准备步骤和方法和以前一样,博主用的工具是XShell ...

  8. spring AspectJ的Execution表达式(转载)

    原文:http://blog.csdn.net/peng658890/article/details/7223046 在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需 ...

  9. python字符串与列表的相互转化

    一.字符串转化为列表 a = 'abcabcabca' a = a.split('c') print(a) #['ab', 'ab', 'ab', 'a'] 二.列表转化为字符串 li = [&quo ...

  10. DevExpress WPF v19.1新版亮点:Ribbon等控件新功能

    行业领先的.NET界面控件DevExpress 日前正式发布v19.1版本,本站将以连载的形式介绍各版本新增内容.在本系列文章中将为大家介绍DevExpress WPF v19.1中新增的一些控件及部 ...