LeetCode(3) - Longest Substring Without Repeating Characters
这题的题意大概就是给你一个字符串"abcdecde",找到最长的子字符串长度,里面所有的子母都不重复。本例子中最长的满足条件的子字符串就是"abcde",所以应该返回的是5。这一题如果不用暴力解决的方法的话,我优先想到的数据结构是HashMap,因为它能够判断字母是否重复,并且记录每个字母的index。而现在唯一的难点就是,怎么通过HashMap来找到这个最长字符串。因为要记录子字符串的长度,从而,肯定需要两个index,一个index1作为子字符串的头,另一个index2再往后移动,直到index2碰到index1和index2之间出现过的元素(假设位置为i),则记录length = index2 - index1(不需要加1是因为index2是重复元素,所以得以index2-1来算),同时,要把HashMap里面index1和i之间的元素给删掉,index1从i+1开始,index2继续走下去。每一次记录length,都要判断是否比之前最长的length要长,如果是,就更新它,不是,就跳过。
这里还有一个边界条件。就是,当index2走完整个字符串的时候,length并不会被记录,所以在循环外得判断一遍。
代码如下:
public class Solution {
public int lengthOfLongestSubstring(String s) {
if (s.length() == 0 || s.length() == 1) return s.length();
HashMap<Character,Integer> map = new HashMap<Character, Integer>();
//length为最大长度,head为当前子字符串的头指针。
int length = 0;
int head = 0;
//把第一个字母放到map里。
map.put(s.charAt(head),head);
for (int i = 1; i < s.length(); i++) {
char c = s.charAt(i);
//contain的话,说明i和head之间存在与c相同的元素。
if (map.containsKey(c)) {
//找出重复元素的位置index。并去掉map里面index前所有的字符(包括该字符)
int index = map.get(c);
for (int j = head; j <= index; j++) {
map.remove(s.charAt(j));
}
//记录并比较当前最长的length
length = Math.max(length,i - head);
//head从index+1开始(index和i重复,所以从index+1开始)
head = index+1;
}
map.put(c,i);
}
//最后再判断前面说的边界条件,上面循环并没有记录i走到字符串尾时的子字符串长度。
if (head != s.length() - 1) {
length = Math.max(length, s.length() - head);
}
return length;
}
}
LeetCode(3) - Longest Substring Without Repeating Characters的更多相关文章
- 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters
一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...
- 【LeetCode OJ】Longest Substring Without Repeating Characters
题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...
- 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 【LeetCode】3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- leetcode题解 3. Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- UVA 11427 Expect the Expected (期望)
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=26&pa ...
- open_binary_frm
参数uchar* head 是已经分配好内存的64个字节的地址 http://mysql.taobao.org/monthly/2015/08/07/ /** *先从.frm文件读取64字节 *第28 ...
- 中文web font技术及方案
我们在日常需求中,经常会碰到视觉设计师对某个中文字体效果非常坚持的情况,因为页面是否高大上,字体选择是很重要的一个因素,选择合适的字体可以让页面更优雅.面对这种问题,我们通常以下方式来进行设计还原: ...
- wince6下载地址
http://geekswithblogs.net/WindowsEmbeddedCookbook/archive/2010/08/31/installing-windows-ce-6.0-tools ...
- window下python 扩展库安装 使用第三方镜像源
0.前言 由于python的官方镜像位于国外,若使用pip或者easy_install安装第三方插件时或许会被限制,甚至连easy_install或pip也无法安装,例如在windows环境下 ...
- ubuntu下实现openerp 7使用nginx反正代理及绑定域名
这里要记录一个nginx upstream实现反向代理的配置过程. 连接vps的ssh. 先安装nginx sudo apt-get install nginx 修改/etc/nginx/nginx. ...
- 使用 github.io 免费建站
/*************************************************************************** * 使用 github.io 免费建站 * 说 ...
- Android 源代码自动编译packages/apps
/*************************************************************************** * Android 源代码自动编译packag ...
- Google Code Pretiffy 代码 着色 高亮 开源 javascript(JS)库
1.简介 introduction Google Code Pretiffy 是 Google 的一个用来对代码进行语法着色的 JavaScript 库,支持 C/C++, Java, Python, ...
- 计算机视觉入门 Intorduction To Computer Vision
本文将主要介绍图像分类问题,即给定一张图片,我们来给这张图片打一个标签,标签来自于预先设定的集合,比如{people,cat,dog...}等,这是CV的核心问题,图像分类在实际应用中也有许多变形,而 ...