【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, ...
随机推荐
- angualrJs实现图片上传功能
整体逻辑:service提供FileReader函数,directive提供点击事件的绑定和监听,controller用来修改html上的ng-src属性值 1.HTML <input type ...
- JQuery和原生JavaScript实现网页定位导航特效
慕课网的一个小课程,练习了一遍,不足之处,欢迎指正(照片在本地,大家可以着重看代码哈): <!DOCTYPE html> <html lang="en"> ...
- Android自定义Button按钮显示样式
关于listview和button都要改变android原来控件的背景,在网上查找了一些资料不是很全,所以现在总结一下android的selector的用法. 首先android的selector是在 ...
- 异度之刃 Xenoblade 后感
WII版重置的N3DS劣化版异度之刃终于通关了.在出色的自制系统的快乐NTR的帮助下,充分体验到了神作的剧情史诗感. 关于游戏的玩法系统,从现在来看8年前的游戏,缺点显而易见,特别是跑地图这回事,地图 ...
- centos7 部署 汉化版 gitlab 10.0.2
更新说明: 20171009:增加3.5的内容 20171008:整理出gitlab部署手册 =============================================== gitla ...
- 查看windows所有exe的启动参数。
在cmd中输入 wmicprocess 即可查看到所有进程的启动参数和运行参数.
- JQuery 常用命令总结
下面介绍在jQuery中设置form表单中action的值的方法. $("#myFormId").attr("action", "userinfo.s ...
- mvc 下json超过限制,上传excel大小限制
json超过限制 解决方案1:config中添加节点 控制序列化长度 <webServices> <jsonSerialization maxJsonLength="10 ...
- robotFramework--ride 问题:Data source does not exist.
第一次安装robotFramework,运行时提示Data source does not exist.最后发现是在Arguments这一栏误输入了. 导致的,去掉.后就可以正常运行了.
- Ogre学习教程:Ogre1.8.1+VS2010环境配置(转)
http://blog.csdn.net/yangtrees/article/details/8724120 http://blog.csdn.net/cll611/article/details/8 ...