题目描述:

解题思路:

  借用网上大神的思想:the basic idea is, keep a hashmap which stores the characters in string as keys and their positions as values, and keep two pointers which define the max substring. move the right pointer to scan through the string , and meanwhile update the hashmap. If the character is already in the hashmap, then move the left pointer to the right of the same character last found. Note that the two pointers can only move forward.

  大意:基本思想是,用一个hashmap存储字符串,把字符串的每一个字符当作key,字符所在的位置作为value,并维护两个指针,两个指针之间就是不存在重复字符的字串,而此题求的是满足这样要求的最大字串。然后,移动右指针遍历字符串,同时更新hashmap。如果遍历到的字符已存在于hashmap,就移动左指针到最后一次出现该字符的右边一个位置。注意,两个指针都只能向右移动,不能回退。

  以字符串abbc为例:

Java代码:

 import java.util.HashMap;
import java.util.Map; public class LeetCode371 {
public static void main(String[] args) {
String s="abba";
System.out.println(s+"最长不存在重复字符的字串长度是:"+new Solution().lengthOfLongestSubstring(s));
}
}
class Solution {
public int lengthOfLongestSubstring(String s) {
Map<Character,Integer> map=new HashMap<Character,Integer>();
int max=0;
for(int left=0,right=0;right<s.length();right++){
if(map.containsKey(s.charAt(right)))
left=Math.max(left,map.get(s.charAt(right))+1);
map.put(s.charAt(right), right);
max=(right-left+1)>=max?(right-left+1):max;
}
return max;
}
}

程序结果:

【LeetCode3】Longest Substring Without Repeating Characters★★的更多相关文章

  1. 【LeetCode】Longest Substring Without Repeating Characters 解题报告

    [题意] Given a string, find the length of the longest substring without repeating characters. For exam ...

  2. 【leetcode】Longest Substring Without Repeating Characters

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

  3. 【leetcode】Longest Substring Without Repeating Characters (middle)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  4. 【Leetcode】【Medium】Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  5. 【LeetCode】Longest Substring Without Repeating Characters(无重复字符的最长子串)

    这道题是LeetCode里的第3道题. 题目描述: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: ...

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

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

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

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

  8. 【LeetCode OJ】Longest Substring Without Repeating Characters

    题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...

  9. 【leetcode刷题笔记】Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

随机推荐

  1. 依赖注入(一)构造函数注入(PHP)

    构造函数注入(constructor injection)是依赖注入最常见的形式之一. 由名称可以看出,该技术需要我们把所有依赖显示的体现在构造函数中. 好了,直接上代码: <?php /** ...

  2. 百万级数据 MySQL处理(转)

    转自 http://www.cnblogs.com/win7xt/p/3156334.html 使用MySQL处理百万级以上数据时,不得不知道的几个常识   最近一段时间参与的项目要操作百万级数据量的 ...

  3. 关于session销毁的问题,invalidate() 和removeAttribute()

    request.getSession().invalidate(); 销毁当前会话域中的所有属性 request.getSession().removeAttribute("username ...

  4. Java设计模式—建造者模式

    建造模式:        将一个复杂的对象的构建与它的表示分离,使得同样的构建 过程可以创建不同的. 建造模式表示是将复杂的内部创建封装在内部,对于外部调用的人来说,只需要传入建造者和建造工具,对于内 ...

  5. c# 将秒数转换成时,分,秒的方法

    TimeSpan ts = , ,Convert.ToInt32( duration)); string str = ""; ) { str = ts.Hours.ToString ...

  6. 关于p标签

    说p标签是不能嵌套div和p的,嵌套会被浏览器解析分离.但如果你使用了document.createElement创建div,再appendChild的话反而可以了.看来浏览器并不支持动态解析

  7. C# 调用WebService服务

    方 法 一 :  选择项目,右键添加服务引用,输入服务地址,如图: 点击确定跳转到如下界面: 调用 接口: ServiceReference1.ImportDataServiceClient sr = ...

  8. JS 触发不同ifram控件,实现刷新,关闭标签(H+框架)

    例: //前台页面事件处理模块var EventOperation = { Refresh: function (data_id) { var a = (window.top); var ele = ...

  9. 配置consul为windows服务

    安装consul并配置为系统服务下载地址https://www.consul.io/downloads.html 配置系统服务1.拷贝consul.exe的目录 如:E:\Consul\consule ...

  10. 用标签页TitleSwitch切换不通的控制器

    用标签页TitleSwitch切换不通的控制器 教程效果: 项目开发中效果: 各种源码: TitleSwitch.h 与 TitleSwitch.m (这个是修改过的升级版本) // // Title ...