【LeetCode3】Longest Substring Without Repeating Characters★★
题目描述:

解题思路:
借用网上大神的思想: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★★的更多相关文章
- 【LeetCode】Longest Substring Without Repeating Characters 解题报告
[题意] Given a string, find the length of the longest substring without repeating characters. For exam ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
- 【leetcode】Longest Substring Without Repeating Characters (middle)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【Leetcode】【Medium】Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【LeetCode】Longest Substring Without Repeating Characters(无重复字符的最长子串)
这道题是LeetCode里的第3道题. 题目描述: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: ...
- 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Example 1 ...
- 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)
Given a string, find the length of the longest substring without repeating characters. Example 1: ...
- 【LeetCode OJ】Longest Substring Without Repeating Characters
题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...
- 【leetcode刷题笔记】Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- php写入文件fwrite() 函数用法
在php中,php fwrite() 函数是用于写入文件(可安全用于二进制文件).说的简单点,就是在一个文件中,添加新的内容,本篇文章收集总结了几篇关于php写入文件fwrite() 函数用法的总结, ...
- <Android 基础(二十六)> 渐变色圆角Button
简介 总结下之前看的自定义View的内容,结合一个简单的例子,阐述下基本用法和大致的使用流程,这个例子比较简单,更复杂的自定义View,随着自己的学习,后面再慢慢添加.作为一个Android开发者,这 ...
- WPF tooltip 根据父元素属性决定是否显示
例如:Placement为disable的时候,需要显示一段tooltip, 而当Placement为enable的时候,不显示 <Button Grid.Row="2" x ...
- Spring框架学习之概述
一.什么是软件框架 打个比方就像建筑的混凝土框架,搭好了架子,工人只需要把墙填上. .特点: -半成品 -封装了特定的处理流程和控制逻辑 -成熟的,不断升级改造的软件 .框架和类库的区别 -框架一般是 ...
- c# 设计模式 之:策略模式
算法与对象的耦合: 对象可能经常需要使用多种不同的算法,但是如果变化频繁,会将类型变得脆弱... 动机: 在软件构建过程中,某些对象使用的算法可能多种多样,经常 ...
- JWT能够干什么,不应该干什么?
http://cryto.net/~joepie91/blog/2016/06/13/stop-using-jwt-for-sessions/ At the start of this article ...
- SSL Certificates深入理解
http://www.littlewhitedog.com/content-71.html https://www.verisign.com/en_US/website-presence/websit ...
- Ehcache.xml 配置及属性说明
1.配置样例 <?xml version="1.0" encoding="UTF-8"?> <ehcache> <diskStor ...
- [UI] 精美UI界面欣赏[11]
精美UI界面欣赏[11]
- Git提交代码自动触发JenKins构建项目
1.需求场景 用户提交代码后自动触发jenkins构建项目 流程图如下: 2.JenKins安装Gitlab Hook Plugin插件 3.JenKins配置 4.Gitlab Hook Plugi ...