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.


题解:贪心算法。

利用一个hashmap存放当前最长的无重substring所包含的元素,变量leftbound存放当前的最左边界。则当前遍历的字符s[i]有两种情况:

  1. s[i]所指的元素已经存在在map中了,那么就左移leftbound直到leftbound到i这一段不再包含s[i],并且在map中清除leftbound扫过的元素;
  2. s[i]所指的元素不在map中,保持leftbound不动,查看是否需要更新当前最长substring的长度。

代码如下:

 public class Solution {
public int lengthOfLongestSubstring(String s) {
if(s == null || s.length() == 0)
return 0; HashMap<Character, Integer> map = new HashMap<Character, Integer>();
int leftebound = 0;
int answer = 0; for(int i = 0;i < s.length();i++){
char current = s.charAt(i);
if(map.containsKey(s.charAt(i))){
while(s.charAt(leftebound) != current && leftebound < i){
map.remove(s.charAt(leftebound));
leftebound++;
}
leftebound++;
}
else {
map.put(current, 0);
answer = Math.max(i-leftebound+1, answer);
}
} return answer;
}
}

【leetcode刷题笔记】Longest Substring Without Repeating Characters的更多相关文章

  1. 刷题3. Longest Substring Without Repeating Characters

    一.题目 Longest Substring Without Repeating Characters,具体请自行搜索. 这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂. 但要做到bug ...

  2. 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters

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

  3. 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)

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

  4. Leetcode第三题《Longest Substring Without Repeating Characters》

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

  5. 【LeetCode】3 、Longest Substring Without Repeating Characters

    题目等级:Medium 题目描述:   Given a string, find the length of the longest substring without repeating chara ...

  6. Leetcode经典试题:Longest Substring Without Repeating Characters解析

    题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...

  7. LeetCode(3)Longest Substring Without Repeating Characters

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

  8. 【leetcode刷题笔记】Substring with Concatenation of All Words

    You are given a string, S, and a list of words, L, that are all of the same length. Find all startin ...

  9. [刷题] 3 Longest Substring Without Repeating Character

    要求 在一个字符串中寻找没有重复字母的最长子串 举例 输入:abcabcbb 输出:abc 细节 字符集?字母?数字+字母?ASCII? 大小写是否敏感? 思路 滑动窗口 如果当前窗口没有重复字母,j ...

  10. LeetCode Hash Table 3. Longest Substring Without Repeating Characters

    HashMap的应用可以提高查找的速度,键key,值value的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决.而本题需要解决的问题就是判断新遍历到的字 ...

随机推荐

  1. 谈谈WEB开发中的苦大难字符集问题

    http://www.lanceyan.com/tech/arch/web_luanma.html记得刚做javaweb开发的时候被这个编码问题搞得晕头转向,经常稀里糊涂的编码正常了一会编码又乱了.那 ...

  2. android下拉菜单spinner的使用方法

    Spinner控件也是一种列表类型的控件,它的继承关系如下:  java.lang.Object    ↳ android.view.View      ↳ android.view.ViewGrou ...

  3. [redis]redis概述

    Redis是一个开源.支持网络.基于内存.可持久化的日志型.key-value键值对数据库.使用ANSI C编写.并提供多种语言的API. 它是远程字典server(remote dictionary ...

  4. linux LVS (keepalived+ipvsadm)负载均衡搭建

    ipsvadm 配置 一:安装配置 LVS+DR+Keepalivedclient 访问的地址 VIP 192.168.133.100       负载服务器master真实IP        192 ...

  5. [Erlang危机](5.1.1)内存

    原创文章,转载请注明出处:server非业余研究http://blog.csdn.net/erlib 作者Sunface , and some of the hidden data I mention ...

  6. 简洁方便的集合处理——Java 8 stream流

    背景 java 8已经发行好几年了,前段时间java 12也已经问世,但平时的工作中,很多项目的环境还停留在java1.7中.而且java8的很多新特性都是革命性的,比如各种集合的优化.lambda表 ...

  7. distcc加速内核编译

    Linux内核编译实在是费时间的事,搞内核移植的时候总要编译,生命有一部分就浪费在等内核编译完成上,有心想买个HP的工作站,看了下Z840的价格,想想还是算了.distcc早就听说过,一直没有去试试, ...

  8. 【数据挖掘】聚类之k-means(转载)

    [数据挖掘]聚类之k-means 1.算法简述 分类是指分类器(classifier)根据已标注类别的训练集,通过训练可以对未知类别的样本进行分类.分类被称为监督学习(supervised learn ...

  9. ConcurrentHashMap的使用和原理

    呵呵呵,原理nmb. HashTable,HashMap,ConcurrentHashMap 当你作为一个菜鸡的时候,别人就会那这个来问你. 为什么要用ConcurrentHashMap,因为Hash ...

  10. php编译安装与配置

    php编译安装与配置 =========================================== 官网:http://php.net/ 官网下载:http://php.net/downlo ...