LeetCode OJ-3.Longest Substring Without Repeating Characters(最长无重复子串)
来源:互联网 发布:众银家苹果下载软件 编辑:程序博客网 时间:2024/06/10 20:01
LeetCode OJ-3.Longest Substring Without Repeating Characters(最长无重复子串)
题目描述
3. Longest Substring Without Repeating Characters
Add to List
QuestionEditorial Solution
My Submissions
- Total Accepted: 227666
- Total Submissions: 964454
- Difficulty: Medium
- Contributors: Admin
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given
"abcabcbb"
, the answer is"abc"
, which the length is 3.Given
"bbbbb"
, the answer is"b"
, with the length of 1.Given
"pwwkew"
, the answer is"wke"
, with the length of 3. Note that the answer must be a substring,"pwke"
is a subsequence and not a substring.
题目理解
题目简单来说就是求最长无重复子串,子串是连续的。做起来就比较简单,但要注意细节:1.要记录最长的无重复子串起始与结尾位置,以便求解最长长度;2.出现在未出现重复的情况下,要及时更新最大长度;3.检测重复。简单的实现方法时间复杂度O(n^2),空间复杂度O(1),具体代码如下:
Code
#define MAX(m, n) ( (m > n) ? m : n ) int get_longest_substring_len(const char *str, size_t sz){ int max_len = 0; int i = 0; //起始位置 int j = 0; //结束位置 int k = 0; while (j < sz) { k = i; while (k < j) { //检测j之前是否存在重复元素 if (str[k] == str[j]) { //存在重复元素则跳出循环 break; } else { ++k; } } if (k == j) { //j之前未检测到重复元素 max_len = MAX(max_len, j - i + 1); ++j; } else { //j之前存在重复元素 ++i; j = i; } } return max_len;}class Solution {public: int lengthOfLongestSubstring(string s) { size_t sz = s.length(); return get_longest_substring_len(s.c_str(), sz); }};
0 0
- LeetCode OJ-3.Longest Substring Without Repeating Characters(最长无重复子串)
- LeetCode OJ 之 Longest Substring Without Repeating Characters (无重复的最长子串)
- [LeetCode]3. Longest Substring Without Repeating Characters 最长无重复子串解析。java
- LeetCode OJ:Longest Substring Without Repeating Characters(最长不重复子串)
- leetcode:Longest Substring Without Repeating Characters (寻找最长无重复字符的子串)
- LeetCode-3 Longest Substring Without Repeating Characters(最长无重复子串)
- 【LeetCode】4. Longest Substring Without Repeating Characters 最长无重复子串
- leetcode 3 : Longest Substring Without Repeating Characters 最长无重复子串 (C# 语言版)
- leetcode 3 Longest Substring Without Repeating Characters最长无重复子串
- [Leetcode #3]Longest Substring Without Repeating Characters 无重复字符的最长子串
- LeetCode-3:Longest Substring Without Repeating Characters (最长无重复字符的子串) --medium
- 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(最长不重复子串)
- 3. Longest Substring Without Repeating Characters 无重复字符的最长子字符串
- 寻找最长无重复字符子串 Longest Substring Without Repeating Characters
- 解析php函数method_exists()与is_callable()的区别
- 把数据库的文件导出EXCEL表格
- 专业名词
- 264. Ugly Number II 类别:动态规划 难度:medium
- pod升级以及更新不到最新的第三方库解决方法
- LeetCode OJ-3.Longest Substring Without Repeating Characters(最长无重复子串)
- JavaScript笔记(1)
- android 把socket 提到JNI层实现
- Nginx学习总结(5)——Nginx基本配置备忘
- redis的事件驱动
- logback+slf4j日志框架
- (笔记)VB中修改字体大小、日期加减,oracle数据库中查询条件为日期类型
- 隐式类型转换系统是很好的.
- java 静态方法和实例方法的区别