[LeetCode]-003-Longest Substring Without Repeating Characters

网址:https://leetcode.com/problems/longest-substring-without-repeating-characters/

题意:

求出最长子字符串且不含重复字符

分析:

题意比较简单

只需要注意字符串为空的特殊情况.

字符是char的0-255,不仅仅是26个小写字母.

解法:

一直统计长度,直到有字符被重复为止,再记录被重复的字符的下一位

重复该过程

比如:

abcdb

扫描到第二个b,发现前面存在b,那么start就是从第一个b的后一位算,即下标2算起.

当第一个结点发生时,长度4.

当结束后,再统计一次,长度3,比较,记录4.

理解KMP,这个题就很好解...类似于KMP的变形吧

类KMP+计数

代码:

https://github.com/LiLane/leetcode/blob/master/c%2B%2B/003-LongestSubstringWithoutRepeatingCharacters-201504232112.cpp

https://github.com/LiLane/leetcode/blob/master/java/003-LongestSubstringWithoutRepeatingCharacters-201504232111.java

时间: 2024-08-26 12:13:52

[LeetCode]-003-Longest Substring Without Repeating Characters的相关文章

Java for LeetCode 003 Longest Substring Without Repeating Characters

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 subst

[LeetCode] 003. Longest Substring Without Repeating Characters (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 003.Longest_Substring_Without_Repeating_Characters (Medium) 链接: 题目:https://oj.leetcode.com/problems/Longest-Substring-Without-Repeating-Characters/ 代码(github)

【LeetCode】003 Longest Substring Without Repeating Characters

题目:LeetCode 003 Longest Substring Without Repeating Characters 题意:给一个字符串,找到一个没有重复字符的最长子串. 样例:”bbbbb” 返回”b”的长度1:”abcabcbb”返回”abc”的长度3. 思路: 动态规划.dp[i]表示以第i个字符为结尾的无重复字符的子串的最长的长度,需要一个辅助数组idx[s[i]]记录字符当前字符s[i]上一次出现的位置,如果未出现过则为-1.所以得到如下的递推公式: 另外由于不确定字符串长度的

【leetcode】 Longest Substring Without Repeating Characters

题目: 给定一个字符串,返回该串没有重复字符的最长子串. 分析: 1)子串:子串要求是连续的. 2)无重复,出现重复就断了,必须从新的位置开始.而新的位置就是重复字符第一次出现位置的下一个位置. 3)整个串可能没有一处重复. 那么,为了找出当前访问的字符是否出现过,要怎么做呢?当然是hash,O(1)的时间,而且既然是字符, 定义个255的hash table 就可以了,hash table 中的元素为相应字符在s中出现的位置.初始化为-1,表示都没有出现. 我们另外定义一个start 和end

leetcode -day21 Longest Substring Without Repeating Characters

 1.Longest Substring Without Repeating Characters 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

LeetCode-Algorithms #003 Longest Substring Without Repeating Characters, Database #177 Nth Highest Salary

LeetCode-Algorithms #003 Longest Substring Without Repeating Characters 对于给定的字符串, 找出其每个字符都不重复的子串中最长的那个, 并返回该子串的长度: 想法还是遍历: 1 class Solution { 2 public int lengthOfLongestSubstring(String s) { 3 // 如果s是null或空串, 就直接返回0 4 if (s == null || "".equals

[LeetCode] 5. Longest Substring Without Repeating Characters 最长回文子串

[LeetCode] 5. Longest Substring Without Repeating Characters Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example 1: Input: "babad" Output: "bab" Note: "aba&qu

[LeetCode][Python]Longest Substring Without Repeating Characters

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, find the length of the longest substring without repeating characters.For example, the longest s

[LeetCode][JavaScript]Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters 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 lengt

Leetcode 3 Longest Substring Without Repeating Characters. (最长无重复字符子串) (滑动窗口, 双指针)

目录 问题描述 例子 方法 Leetcode 3 问题描述 Given a string, find the length of the longest substring without repeating characters. 例子 Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: &q