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, ...
随机推荐
- django中post方法和get方法的不同
当我们提交表单仅仅需要获取数据时就可以用GET: 而当我们提交表单时需要更改服务器数据的状态,或者说发送e-mail,或者其他不仅仅是获取并显示数据的时候就使用POST. 在这个搜索书籍的例子里,我们 ...
- php注册登录系统(一)-极简
序 登录注册系统是日常上网最普通的操作,我设了一个分类一步步完善注册登录系统,若有哪里错误请慧教 所用语言:php 数据库 :mysql 本次实现功能: 1.用户注册 2.用户登录 主要文件: 完整代 ...
- What is Entity Framework?
1.什么是EntityFramework? http://www.entityframeworktutorial.net/what-is-entityframework.aspx Writing an ...
- 第六讲(二) Hibernate HQL查询
HQL查询:Criteria查询对查询条件进行了面向对象封装,符合编程人员的思维方式,不过HQL(Hibernate Query Lanaguage)查询提供了更加丰富的和灵活的查询特性,因此Hibe ...
- No resource found that matches the given name
XML里面明显已经定义了ID,可是android:layout_toLeftOf="@id/text_seller"报错,说没有定义,原来这玩意要写在相对位置对象声明的下面,是有顺 ...
- POJ 3107 Godfather (树形dp)
题目链接 虽然题目不难,但是1A还是很爽, 只是刚开始理解错题意了,想了好久. 还有据说这个题用vector会超时,看了以后还是用邻接吧. 题意: 给一颗树,保证是一颗树,求去掉一个点以后的联通块里节 ...
- POJ 1837 Balance 【DP】
题意:给出一个天平,给出c个钩子,及c个钩子的位置pos[i],给出g个砝码,g个砝码的质量w[i],问当挂上所有的砝码的时候,使得天平平衡的方案数, 用dp[i][j]表示挂了前i个砝码时,平衡点为 ...
- Android Terminal telnet windows
/******************************************************************************************** * Andr ...
- acdream 1682 吃不完的糖果(环形最大子段和)
Problem Description 娜娜好不容易才在你的帮助下"跳"过了这个湖,果然车到山前必有路,大战之后必有回复,大难不死,必有后福!现在在娜娜面前的就是好多好多的糖果还有 ...
- matplotlib 绘制柱状图的几个例子
1 error bar #!/usr/bin/env python # a bar plot with errorbars import numpy as np import matplotlib.p ...