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、用一个数组来记录字符是否出现。2、当一个字符重复了,那么当前可以得到一个可能是最长的无重复字符的子字符。
#include <iostream> #include <string> using namespace std; int lengthOfLongestSubstring(string s) { int n = s.length(); int i = 0, j = 0; int maxLen = 0; bool exist[256] = { false }; //用来记录每个字符是否已经出现 while (j < n) { if (exist[s[j]]) { //已经出现的字符中,这个是当前不重复的最长字串 maxLen = maxLen>=j-i?maxLen:j-i; while (s[i] != s[j]) { exist[s[i]] = false; // i++; } i++; //新的开始位置 j++; //从i到j是不会有重复的 } else { exist[s[j]] = true; j++; } } maxLen = maxLen>= n-i?maxLen:n-i; return maxLen; } int main(){ string s("hello"); cout<<lengthOfLongestSubstring(s); return 0; }
相关推荐
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...
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...
Longest Substring Without Repeating Characters" 描述的是一个经典的计算机编程问题,通常出现在面试或编程竞赛中。这个题目要求我们找到一个字符串中最长的子串,该子串中没有重复的字符。这是一个非常基础但又...
Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring 6.ZigZag Conversion 7.Reverse Integer 8.String To Integer 9.Palindrome Number 10.String To Integer 11....
3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 13. Roman to Integer 15. 3Sum 16. 3Sum Closest 17...
说明 ⽬录 第⼀章 序章 关于 LeetCode 什么是 Cookbook 为什么会写这个开源书 关于书的封⾯ 关于作者 关于书中的代码 ⽬标读者 编程语⾔ ...3. Longest Substring Without Repeating Characters 4. ......
#3:Longest Substring Without Repeating Characters #5:Longest Palindromic Substring 链表 #2:Add Two Numbers 分而治之 #53:Maximum Subarray 队列/集 #3:Longest Substring Without Repeating Characters 优先...
c语言入门 c语言_leetcode题解03-longest-substring-without-repeating-characters
Characters.cpp │ │ ├── 004 - Median of Two Sorted Arrays.cpp │ │ └── 005 - Longest Palindromic Substring.cpp │ └── python │ ├── 001 - Two Sum.py │ ├── 002 - Add Two...
js js_leetcode题解3-longest-substring-without-repeating-characters.js
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.
Without Repeating Characters 4. Median of Two Sorted Arrays 5. Longest Palindromic Substring 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 12. Integer to Roman 13. Roman to ...
Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 Longest Palindromic Substring 中等 回文 ZigZag Conversion 中等 矩阵 重要 Reverse Integer 简单 字串 String to Integer ...
Characters4. Median of Two Sorted Arrays 004. Median of Two Sorted Arrays 005. Longest Palindromic Substring 006. ZigZag Conversion 007. Reverse Integer 008. String to Integer 009. Palindrome Number ...
Repeating Characters 31.1% Medium 0004 Median of Two Sorted Arrays 30.7% Hard 0005 Longest Palindromic Substring 30.1% Medium 0006 ZigZag Conversion 37.5% Medium 0007 Reverse Integer 25.8% Easy 0008 ...
Without Repeating Characters 5.Longest Palindromic Substring 20.Valid Parentheses 26.Remove Duplicates from Sorted Array 53.Maximum Subarray 70.Climbing Stairs 121.Best Time to Buy and Sell Stock 122....
Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7.Reverse Integer 8.String to Integer (atoi) 9.Palindrome Number 10....
java lru leetcode Fighting for a job! 记录找工作期间搬运的题,全部使用Java实现,本人C++鶸 1 ...Longest Substring Without Repeating Characters LeetCode 13 Roman to Integer LeetCode 6 Zi
Longest Substring Without Repeating Characters 哈希表 双指针 滑动窗口 Substring with Concatenation of All Words 哈希表 注意匹配方向 Valid Sudoku 数组 遍历 Sudoku Solver 深度优先遍历 回溯 先检查后修改 ...