LeetCode 题解 3. Longest Substring Without Repeating Characters(最长不含重复字符的子字符串)
2017-06-17 16:19
495 查看
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/73381326冷血之心的博客)
题目链接地址:https://leetcode.com/problems/longest-substring-without-repeating-characters/#/description
题目的大意是说:请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度。假设该字符串中只包含‘a’-'z'的字符。
打开tag瞅瞅,如下:
于是我们使用了HashMap和双指针来解决该问题。
思路:HashMap中不断更新维护每个字符出现的位置。使用count变量进行计数,当统计的最长子字符串被重复字符破坏时,比较count和max的大小,判断是否需要更新max变量。
总结:不管是哪种情况,每次都需要更新map中的信息,并且需要更新count变量;
(切记切记)
java实现如下:
package pak3;
import java.util.HashMap;
import java.util.Map;
/**
* 3. Longest Substring Without Repeating Characters
* 求最长不含重复字符的子字符串
* @author ywq
*
*
*/
public class Main {
public static void main(String[] args) {
System.out.println(lengthOfLongestSubstring("arbacacfr"));
System.out.println(lengthOfLongestSubstring("hkcpmprxxxqw"));
System.out.println(lengthOfLongestSubstring("dvdf"));
System.out.println(lengthOfLongestSubstring("tmmzuxt"));
System.out.println(lengthOfLongestSubstring("jbpnbwwd"));
}
public static int lengthOfLongestSubstring(String s) {
if(s==null||s.length()==0)
return 0;
// 建立一个HashMap用来存放字符和位置信息
Map<Character,Integer> map = new HashMap<Character, Integer>();
int max = 0; // 用来记录最大值
int count = 0; // 用来统计长度
char[] c = s.toCharArray();
for(int i =0;i<c.length;i++){
if(!map.containsKey(c[i])){
map.put(c[i], i);
count++;
}else {
// 若map中已经包含该字符,分为两种情况讨论
Integer index = map.get(c[i]);
// 情况1:上次出现的该字符并不在当前所统计的最长字符串中,只需要更新位置信息。并且统计count++
if(i-index>count){
count++;
map.put(c[i], i);
continue;
}
// 情况2:上次出现的该字符影响了当前最长不重复的子字符串
// 则更新位置信息、max变量和count计数
map.put(c[i], i);
if(count>max){
max = count;
}
count = i - index;
}
}
// 防止出现没有重复字符的情况,此时max = 0
return max>count?max:count;
}
}
LeetCode AC图:
如果对你有帮助,记得点赞哦~欢迎大家关注我的博客,可以进群366533258一起交流学习哦~
本群给大家提供一个学习交流的平台,内设菜鸟Java管理员一枚、精通算法的金牌讲师一枚、Android管理员一枚、蓝牙BlueTooth管理员一枚、Web前端管理一枚以及C#管理一枚。欢迎大家进来交流技术。
题目链接地址:https://leetcode.com/problems/longest-substring-without-repeating-characters/#/description
题目的大意是说:请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度。假设该字符串中只包含‘a’-'z'的字符。
打开tag瞅瞅,如下:
于是我们使用了HashMap和双指针来解决该问题。
思路:HashMap中不断更新维护每个字符出现的位置。使用count变量进行计数,当统计的最长子字符串被重复字符破坏时,比较count和max的大小,判断是否需要更新max变量。
总结:不管是哪种情况,每次都需要更新map中的信息,并且需要更新count变量;
(切记切记)
java实现如下:
package pak3;
import java.util.HashMap;
import java.util.Map;
/**
* 3. Longest Substring Without Repeating Characters
* 求最长不含重复字符的子字符串
* @author ywq
*
*
*/
public class Main {
public static void main(String[] args) {
System.out.println(lengthOfLongestSubstring("arbacacfr"));
System.out.println(lengthOfLongestSubstring("hkcpmprxxxqw"));
System.out.println(lengthOfLongestSubstring("dvdf"));
System.out.println(lengthOfLongestSubstring("tmmzuxt"));
System.out.println(lengthOfLongestSubstring("jbpnbwwd"));
}
public static int lengthOfLongestSubstring(String s) {
if(s==null||s.length()==0)
return 0;
// 建立一个HashMap用来存放字符和位置信息
Map<Character,Integer> map = new HashMap<Character, Integer>();
int max = 0; // 用来记录最大值
int count = 0; // 用来统计长度
char[] c = s.toCharArray();
for(int i =0;i<c.length;i++){
if(!map.containsKey(c[i])){
map.put(c[i], i);
count++;
}else {
// 若map中已经包含该字符,分为两种情况讨论
Integer index = map.get(c[i]);
// 情况1:上次出现的该字符并不在当前所统计的最长字符串中,只需要更新位置信息。并且统计count++
if(i-index>count){
count++;
map.put(c[i], i);
continue;
}
// 情况2:上次出现的该字符影响了当前最长不重复的子字符串
// 则更新位置信息、max变量和count计数
map.put(c[i], i);
if(count>max){
max = count;
}
count = i - index;
}
}
// 防止出现没有重复字符的情况,此时max = 0
return max>count?max:count;
}
}
LeetCode AC图:
如果对你有帮助,记得点赞哦~欢迎大家关注我的博客,可以进群366533258一起交流学习哦~
本群给大家提供一个学习交流的平台,内设菜鸟Java管理员一枚、精通算法的金牌讲师一枚、Android管理员一枚、蓝牙BlueTooth管理员一枚、Web前端管理一枚以及C#管理一枚。欢迎大家进来交流技术。
相关文章推荐
- LeetCode: 3_Longest Substring Without Repeating Characters | 求没有重复字符的最长子串的长度 | Medium
- [leetcode]Longest Substring Without Repeating Characters【最长不重复字符子串】
- 【LeetCode】003 Longest Substring Without Repeating Characters 最长的没有重复的子字符串
- LeetCode 3. Longest Substring Without Repeating Characters--不包含重复字符的最长子串长度
- [LeetCode]3. Longest Substring Without Repeating Characters寻找最长无重复字符的子串
- [LeetCode] Longest Substring Without Repeating Characters 无重复字符的最长子串
- Leet Code 3. Longest Substring Without Repeating Characters (最长的没有重复字符的子字符串)
- LeetCode题库解答与分析——#3.无重复字符的最长子串LongestSubstringWithoutRepeatingCharacters
- 3. Longest Substring Without Repeating Characters(计算不含重复字符的最长子串的长度)
- leetcode:Longest Substring Without Repeating Characters (寻找最长无重复字符的子串)
- LeetCode-3:Longest Substring Without Repeating Characters (最长无重复字符的子串) --medium
- 3. Longest Substring Without Repeating Characters 无重复字符的最长子字符串
- leetcode——Longest Substring Without Repeating Characters 求链表中无重复字符的最大字串长度(AC)
- Longest Substring Without Repeating Characters (最长不重复字符的子序列) Java-O(n)解法
- leetcode 3 : Longest Substring Without Repeating Characters 最长无重复子串 (C# 语言版)
- LeetCode-3 Longest Substring Without Repeating Characters(最长无重复子串)
- LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
- Leetcode-3 Longest Substring Without Repeating Characters 最长无重复字节字符串
- LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)