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. ubuntu18下lamp虚拟路劲配置

    一.配置二级域名 修改hosts文件,模拟dns解析. 位置:/etc/hosts 添加 127.0.0.1  myweb.service.com 二.创建项目目录 apache默认目录是/var m ...

  2. redis 哈希 数据类型

    哈希 hset  设置哈希表字段 hset 8000 ename tom   hset 8000 job salesman hget 8000 ename  "tom" hget  ...

  3. mysql 表相关操作(1)

    查询语句 select * from t_deptselect empno,ename,sal from t_emp select    empno,   sal * 12 as "inco ...

  4. PythonDay16

    第十六章模块 今日内容 自定义模块 time模块 datetime模块 rendom模块 自定义模块 # import 模块 例:import time# 导入的是time模块中所有的内容(变量,函数 ...

  5. Java Web开发技术教程入门-初识动态网页

    这段时间学校搞了一个"阅战阅勇"的阅读活动,奖品还是挺丰富的~于是,奔着这些奖品,我去图书馆借了这本<Java Web开发技术教程>.一是为了那些丰富的奖品,二是为了回 ...

  6. Ubantu 手动设置DSL连接

    参考链接:https://m.linuxidc.com/Linux/2015-07/119774.htm

  7. MySql 5.7关键字和保留字-附表

    现在使用navicat图形界面或者Hibernate做映射生成表的时候,渐渐的会忽视掉关键字这个问题,而后续也会不断的产生错误提示,一遍遍的查询代码无果,甚至开始怀疑人生,但是其实很多情况下只是使用了 ...

  8. Java 判断是否为回文字符串

    回文字符串有两种:abcba,abccba. 代码: static boolean func(String str) { int len = str.length(); for (int i = 0; ...

  9. ipcs - 提供基于 ipc (Inter-process communication)结构的信息

    总览 ipcrm [ shm | msg | sem ] id 描述 ipcrm 将删除指定的资源--指定 id.

  10. windows server :远程桌面服务当前正忙,因此无法完成您尝试执行的任务

    原因是:Csrss.exe 进程和某些应用程序 (例如,Microsoft Excel 或 Microsoft Visio) 之间发生的死锁情况下会出现此问题. 解决:下载一个修复补丁,安装后重启服务 ...