leetcode Longest Substring Without Repeating Characters Description: Given a string, find the leng
来源:互联网 发布:office办公软件价格 编辑:程序博客网 时间:2024/05/16 08:26
Description:
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 subsequenceand not a substring.
Solution:
1.暴力解法:(超时)
利用枚举思想,列举所有情况。显然时间复杂度达到O(n³)。
2.贪心算法
最长无重复子串肯定包含在两个重复字符之间,用len保存当前最长的子串。如果某两个重复字符之间的子串长于maxLen,则给maxLen赋值这个新的长度。用i,j分别表示头和尾,k用于查找重复。这样只需要2重循环就可以了。
//22msclass Solution {public: int lengthOfLongestSubstring(string s) { int maxLen = 0; int i = 0, j = 0; int n = s.length(); for (; j < n; j++) { bool f = true; for (int k = i; k < j; k++) { if (s[k] == s[j]) { i = k+1; f = false; } } if (f) { maxLen = max(maxLen,j - i + 1); } } return maxLen; }};
阅读全文
0 0
- leetcode Longest Substring Without Repeating Characters Description: Given a string, find the leng
- Given a string, find the length of the longest substring without repeating characters
- [leetcode][string] Longest Substring Without Repeating Characters
- LeetCode-string-Longest Substring Without Repeating Characters
- Leetcode- string/array - Longest Substring Without Repeating Characters
- leetcode 3---Longest Substring Without Repeating Characters 双指针 String
- leetcode-Length of the longest substring without repeating characters
- String-3.Longest Substring Without Repeating Characters
- Find the longest substring with k unique distinct characters in a given string (Google)
- LeetCode: Longest Substring Without Repeating Characters
- LeetCode Longest Substring Without Repeating Characters
- LeetCode: Longest Substring Without Repeating Characters
- [Leetcode] Longest Substring Without Repeating Characters
- leetcode 23: Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- LeetCode 3 - Longest Substring Without Repeating Characters
- [LeetCode]Longest Substring Without Repeating Characters
- leetcode Longest Substring Without Repeating Characters
- 网站地图在线生成
- 从帖子中心开始,聊“1对多”类业务数据库水平切分架构实践
- CODE[VS] 1138 聪明的质检员 二分+前缀和
- 关于"top-level const"与"low-level const"的疑惑
- 问题备忘:解决在debian8系统中Java使用非东八区时区作为默认时区
- leetcode Longest Substring Without Repeating Characters Description: Given a string, find the leng
- java集合之LinkedList
- 屏幕的多点触控
- ionic列表
- NP-complete
- 10-18关于改变对象的移动、淡入淡出、变大变小等功能函数整合
- L2-007. 家庭房产
- Linux Unit7
- Hadoop(二)