题目描述:

解题思路:

  借用网上大神的思想: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. html基础-标题标签-文字标签(2)

    昨天说道了我的第一个网页,今天接着继续带大家深入,前期学习千万不要用代码工具哦!那样就少了深入了解的机会了哦! 一.大家都知道文章会有各种标题,网页其实也跟文章差不多也有专门来写标题的元素. (1). ...

  2. iPhone中调用WCF服务

    本文介绍的是跨平台iPhone中调用WCF服务,WCF是由微软发展的一组数据通信的应用程序开发接口,它是.NET框架的一部分,由 .NET Framework 3.0+开始引入 iPhone中调用WC ...

  3. SqlSugarClientHelper

    using SqlSugar; using System; using System.Collections.Generic; using System.Configuration; using Sy ...

  4. OkHttp2.0有Bug,暂时不推荐在产品中使用

    版权声明: 欢迎转载,但请保留文章原始出处 作者:GavinCT 出处:http://www.cnblogs.com/ct2011/p/4078137.html 之前在博客里推荐使用OkHttp来替换 ...

  5. Oracle EBS AR 删除应收发票

    DECLARE    -- Non-scalar parameters require additional processing     p_errors arp_trx_validate.mess ...

  6. Oracle EBS 查询物料报错

  7. java vector的多线程安全是否有用

    在网上搜了不少文章,发现有不少没讲清楚的,也有不少好文,本文希望更易懂地描述该问题.如有不对的地方,请多多指正~~ vector的使用主要有如下两种场景:(1)vector所谓的多线程安全,只是针对单 ...

  8. ReadWriteLock ReentrantReadWriteLock

    ReadWriteLock管理一组锁,一个是只读的锁,一个是写锁.读锁可以在没有写锁的时候被多个线程同时持有,写锁是独占的. 所有读写锁的实现必须确保写操作对读操作的内存影响.换句话说,一个获得了读锁 ...

  9. C# 输入一个整数,求质因数

    质数,质因数 应该都了解,在这里不过多解释,直接上代码: List<int> results = new List<int>(); int number = Int32.Par ...

  10. SQL Server 数据表代码创建约束

    约束 非空约束 --NN,ont null constraint 必须填写数据不能为空 --指定表 Student 添加名为NN_Student_sClassId非空约束(指定列名sClassId), ...