[leetcode] Longest Substring Without Repeating Characters
来源:互联网 发布:淘宝类目分析模板 编辑:程序博客网 时间:2024/06/09 18:20
From : https://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 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.
class Solution {public: int lengthOfLongestSubstring(string s) { int ans=0, sz=s.size(), idx=0; map<char, int> box; vector<int> pre(sz, -1); for(int i=0; i<sz; i++) { if(box.find(s[i]) != box.end()) pre[i] = box[s[i]]; box[s[i]] = i; } for(int i=0; i<sz; i++) { if(pre[i] != -1 && pre[i]>=idx) {if(ans < i-idx) ans = i-idx; idx = pre[i]+1; } } return max(ans, sz-idx); }};
改进:
class Solution {public: int lengthOfLongestSubstring(string s) { vector<int> locs(256, -1); int idx = -1, max = 0;for (int i=0, sz=s.size(); i<sz; i++) { if (locs[s[i]] > idx) idx = locs[s[i]]; if (i - idx > max) max = i - idx; locs[s[i]] = i; } return max; }};
0 0
- 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
- 【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] Longest Substring Without Repeating Characters
- LeetCode - Longest Substring Without Repeating Characters
- LeetCode 3: Longest Substring Without Repeating Characters
- leetcode-3:Longest Substring Without Repeating Characters
- leetcode之Longest Substring Without Repeating Characters
- Android移动应用开发之MySQL操作
- UVA_2379 Help is needed for Dexter(math)
- ViewPropertyAnimator介绍
- 用 PHP 编写支持高并发的网站
- java 理解Java对象序列化
- [leetcode] Longest Substring Without Repeating Characters
- fibonacci递归算法的“备忘录/Memo”优化法
- 自定义EditText画任意行线并限制文本的物理长度
- CAS简介
- 整理入门笔记_Spring
- 了解 web 及网络基础——极客学院学习笔记
- Xilinx实习一年总结
- 编写网络程序
- bluestacks手机模拟器安装qq或微信时,鼠标左键点聊天编辑框后会自动输入 c 字母