leetcode第3题——**Longest Substring Without Repeating Characters
来源:互联网 发布:大数据电商平台 编辑:程序博客网 时间:2024/06/07 03:26
题目
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
思路
从左向右遍历字符串并作为子串,创建一个字典存储当前子串中各个字符的个数,当判断当前子串有重复的字符时,子串向右滑动1位,即遍历开头的位置加1。以输入字符串'edvldf'为例:当遍历到第2个d时向右滑动1位,从第一个d开始遍历,并且“被滑掉”的字符的个数要减1,取当前子串的长度(遍历下标减去开始位置再加1就是)存入结果res中;然后从第一个d开始遍历又到第二个d时向右滑动1位,从v开始遍历,以此循环一直到遍历完整个字符串。最后返回子串长度的最大值。
代码
Python
class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ start = 0 end = 1 res = end - start count_dic = {} for letter in s: count_dic[letter] = count_dic.get(letter,0) + 1#如果字典已经含当前字符则个数加1,否则其个数为1 while count_dic.get(letter) > 1: count_dic[s[start]] -= 1 start += 1 res = max(res,end - start) end += 1 return res
Java
public class Solution { public int lengthOfLongestSubstring(String s) { int start = 0; int end = 1; int res; if(s.length() == 0) return 0; else res = end - start; Map<Character,Integer> map = new HashMap<Character,Integer>(); for(int i = 0;i < s.length();i++) map.put(s.charAt(i),0); for(int i = 0;i < s.length();i++) { map.put(s.charAt(i),map.get(s.charAt(i)) + 1); //while循环实现子字符串往右滑动 while(map.get(s.charAt(i)) > 1) { map.put(s.charAt(start),map.get(s.charAt(start)) - 1); start += 1; } if(end - start > res) res = end - start; end++; } return res; }}
1 0
- 【LeetCode】LeetCode——第3题: 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
- LeetCode题解——3Longest Substring Without Repeating Characters
- #3 LeetCode——Longest Substring Without Repeating Characters
- leetcode——3 Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters —leetcode第三题
- LeetCode 3 - Longest Substring Without Repeating Characters
- LeetCode 3: Longest Substring Without Repeating Characters
- leetcode-3:Longest Substring Without Repeating Characters
- LeetCode(3) Longest Substring Without Repeating Characters
- leetcode 3LONGEST SUBSTRING WITHOUT REPEATING CHARACTERS
- Leetcode【3】:Longest Substring Without Repeating Characters
- [leetcode 3] Longest Substring Without Repeating Characters
- [Leetcode] 3 - Longest Substring Without Repeating Characters
- LeetCode 3:《Longest Substring Without Repeating Characters》
- [leetcode 3] Longest Substring Without Repeating Characters
- Dragon board 410c硬件简述
- 各种基本排序算法总结
- OpenGL编程指南 (红宝书 第八版) 样例代码配置问题汇总
- 谷歌发布2015年度搜索排行榜
- I²C总线的总结
- leetcode第3题——**Longest Substring Without Repeating Characters
- 字符串的全排列算法讲解
- Keytool工具使用方法
- 张氏读写锁,c++信号量实现
- C++ bitset的使用简介
- 104. Maximum Depth of Binary Tree(菜鸟刷题第一天)
- linux编译驱动之 make modules SUBDIRS
- C++:顺序容器及顺序容器适配器(stack、queue、priority_queue)
- Mysql备份和恢复