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, ...
随机推荐
- Commons-Collections
package com.bjsxt.others.commons; import java.util.ArrayList; import java.util.List; import org.apac ...
- leetcode:Reverse Linked List
Reverse a singly linked list. 代码如下: the iterative solution:(c++) /** * Definition for singly-linked ...
- hibernate工具类HibernateUtil详解
1.为什么要用hibernateUtil这个类,先看这段代码: //加载配置文件信息默认为hiberna.cfg.xml,如果不是的话那么就在config()方法里面去解析他 Con ...
- 解决eclipse-helios中Errors running builder JavaScript Validator的问题
原文:http://blog.ywxyn.com/index.php/archives/713 解决eclipse-helios中Errors running builder JavaScript V ...
- [转]微软联合CSDN英雄在线编程大赛
2014 新年将至,微软联合CSDN英雄会共同举办本次第三届在线编程大赛,题目详情如下: 有一个字符串"iinbinbing",截取不同位置的字符‘b’.‘i’.‘n’.‘g’组合 ...
- [ionic开源项目教程] - 第12讲 医疗模块的实现以及Service层loadMore和doRefresh的提取封装
关注微信订阅号:TongeBlog,可查看[ionic开源项目]全套教程. 这一讲主要实现tab2[医疗]模块,[医疗]模块跟tab1[健康]模块类似. [ionic开源项目教程] - 第12讲 医疗 ...
- 51nod1294 修改数组
看题解的...就是将必须要修改的数去掉后求最长的不递减子序列. upper_bound+lower_bound要理解.有时候-1有时候不用是有原因的. #include<cstdio> # ...
- Linux技巧:一次删除一百万个文件最快方法
昨天,我看到一个非常有趣的删除一个目录下的海量文件的方法.这个方法来自http://www.quora.com/How-can-someone-rapidly-delete-400-000-files ...
- scala学习笔记(9):Scala函数(2)
1 指令式编程&函数式编程 指令式:imperative 风格编程.指令式风格,是你常常使用像 Java,C++和 C 这些语言里用的风格,一次性发出一个指令式的命令,用循环去枚举,并经常改变 ...
- Filling a Path 模式
Filling a Path When you fill the current path, Quartz acts as if each subpath contained in the path ...