leetcode 刷题题解(c++) 3. Longest Substring Without Repeating Characters (快慢指针,字符hash)
来源:互联网 发布:非诚勿扰全灭灯的软件 编辑:程序博客网 时间:2024/06/01 07:59
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.
解法:
class Solution {private: bool canUse[256];public: int lengthOfLongestSubstring(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function memset(canUse, true, sizeof(canUse)); int count = 0; int ret = 0; for(int i = 0, j = 0; i < s.size(), j < s.size(); j++) { if (canUse[s[j]]) { canUse[s[j]] = false; count++; } else { ret = max(ret, count); while (true) { if(s[i] == s[j]) { canUse[s[i]] = false; i ++; break; } else { canUse[s[i]] = true; i ++; } } count = j -i + 1; } } ret = max(ret, count); return ret; }};
0 0
- leetcode 刷题题解(c++) 3. Longest Substring Without Repeating Characters (快慢指针,字符hash)
- Leetcode题解(Python): 3.Longest Substring Without Repeating Characters
- LeetCode - Longest Substring Without Repeating Characters hash
- 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(C++)
- 【leetcode】3. Longest Substring Without Repeating Characters(Python & C++)
- LeetCode题解--3. Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters : LeetCode 题解
- LeetCode题解:3. Longest Substring Without Repeating Characters
- leetcode 题解 3. Longest Substring Without Repeating Characters
- Leetcode题解 - 3. Longest Substring Without Repeating Characters
- LeetCode Algorithms 3. Longest Substring Without Repeating Characters 题解
- leetcode题解-3. Longest Substring Without Repeating Characters
- 3. Longest Substring Without Repeating Characters LeetCode题解
- LeetCode题解(Week 16):3. Longest Substring Without Repeating Characters
- php数据库常用函数的封装
- 【Spring MVC】Spring MVC原理
- 百度地图API的IP定位城市和浏览器定位(转)
- #NOIP 2014# day.1 T2 联合权值
- 【代码笔记】iOS-UILable高度自适应(sizeWithFont)
- leetcode 刷题题解(c++) 3. Longest Substring Without Repeating Characters (快慢指针,字符hash)
- .NET AOP(一)七种AOP实现方法
- odoo中根据模型生成viwe视图的框架代码
- 安卓开发板——网口通信
- 桥接模式
- mysql储存引擎innodb
- Java实现简单AES RSA加密
- SpringMVC的Controller方法参数
- 浅尝JMX