leetcode——Longest Substring Without Repeating Characters 求链表中无重复字符的最大字串长度(AC)
来源:互联网 发布:电脑优化提速 编辑:程序博客网 时间:2024/06/15 22:10
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.
为了提升时间效率,我开辟了一个长度为256的整型数组来标识子串中的字符是否重复,避免了采用循环比较的方法来判定字符是否重复。需要注意的是当我们发现一个字符与当前子串中的字符重复时,此时新的字串需要从子串中第一次出现此字符位置的后一个字符开始作为新的子串的起点,代码中采用了一个整型变量来记录判定过程中的最大子串长度。代码如下:
class Solution {public: int lengthOfLongestSubstring(string s) { int length = s.length(),maxValue=0,count=0;int flag[256];Reset(flag);for(int i=0; i<length; i++){if(flag[s[i]] == -1){count++;flag[s[i]] = i;}else {i = flag[s[i]]+1;Reset(flag);flag[s[i]] = i;count=1;}if(maxValue < count)maxValue = count;}return maxValue; }void Reset(int *flag){for(int i = 0; i<256; i++)flag[i] = -1;}};
0 0
- leetcode——Longest Substring Without Repeating Characters 求链表中无重复字符的最大字串长度(AC)
- Longest Substring Without Repeating Characters 最长不重复字符的字串 @LeetCode
- LeetCode 3. Longest Substring Without Repeating Characters--不包含重复字符的最长子串长度
- leetcode:Longest Substring Without Repeating Characters (寻找最长无重复字符的子串)
- LeetCode OJ Longest Substring Without Repeating Characters 不重复的最长字串 滑动窗口
- Longest Substring Without Repeating Characters (返回最长无字符重复的子串长度)
- 字符的最长无重复子串长度Longest Substring Without Repeating Characters
- 求字符串中连续无重复子字符串的最大长度 (Longest Substring Without Repeating Characters)
- 3. Longest Substring Without Repeating Characters(计算不含重复字符的最长子串的长度)
- [Leetcode #3]Longest Substring Without Repeating Characters 无重复字符的最长子串
- LeetCode-3:Longest Substring Without Repeating Characters (最长无重复字符的子串) --medium
- Longest Substring Without Repeating Characters最大字串问题
- 【2017.11.30】3. Longest Substring Without Repeating Characters-最长字串不重复字符
- LeetCode 题解 3. Longest Substring Without Repeating Characters(最长不含重复字符的子字符串)
- 384.Longest Substring Without Repeating Characters-最长无重复字符的子串(中等题)
- LeetCode OJ 之 Longest Substring Without Repeating Characters (无重复的最长子串)
- Longest Substring Without Repeating Characters(最长的无重复的字串)
- 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复
- 设计模式----抽象工厂模式
- 使用 lsof 查找打开的文件
- Socket通信实验总结
- 广义互相关求信号时延 JAVA实现
- n 个元素顺序入栈,则可能的出栈序列有多少种?
- leetcode——Longest Substring Without Repeating Characters 求链表中无重复字符的最大字串长度(AC)
- All in All
- POJ3254 Corn fiedls
- void用法的总结
- Unix调试的瑞士军刀:lsof
- Web For Pentester 阅读笔记(1)
- GIT远程中央版本库配置
- BF算法
- calculator