Given a string, find the length of the longest substring without repeating characters.

Have you met this question in a real interview?

Yes
Example

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.

Challenge

O(n) time

LeetCode上的原题,请参见我之前的博客Longest Substring Without Repeating Characters

解法一:

class Solution {
public:
/**
* @param s: a string
* @return: an integer
*/
int lengthOfLongestSubstring(string s) {
int res = , left = -;
vector<int> m(, -);
for (int i = ; i < s.size(); ++i) {
left = max(left, m[s[i]]);
m[s[i]] = i;
res = max(res, i - left);
}
return res;
}
};

解法二:

class Solution {
public:
/**
* @param s: a string
* @return: an integer
*/
int lengthOfLongestSubstring(string s) {
int res = , left = , right = ;
unordered_set<char> st;
while (right < s.size()) {
if (!st.count(s[right])) {
st.insert(s[right++]);
res = max(res, (int)st.size());
} else {
st.erase(s[left++]);
}
}
return res;
}
};

[LintCode] Longest Substring Without Repeating Characters的更多相关文章

  1. LeetCode[3] Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...

  2. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  3. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  4. 3. Longest Substring Without Repeating Characters(c++) 15ms

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  5. 【leetcode】Longest Substring Without Repeating Characters

    题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...

  6. Longest Substring Without Repeating Characters(C语言实现)

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  7. leetcode: longest substring without repeating characters

    July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...

  8. [LeetCode_3] Longest Substring Without Repeating Characters

    LeetCode: 3. Longest Substring Without Repeating Characters class Solution { public: int lengthOfLon ...

  9. Longest Substring Without Repeating Characters (c#)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

随机推荐

  1. 真机测试INSTALL_FAILED_INSUFFICIENT_STORAGE 的解决方法

    来源:http://blog.csdn.net/aikongmeng/article/details/9793809 INSTALL_FAILED_INSUFFICIENT_STORAGE 的解决方法 ...

  2. socket编程学习

    socket: 也称作套接字,应用程序通常通过套接字向网络发出请求或者应答网络请求. 常用的套接字API函数: 1.socket(): 函数原型为:int socket(int domain, int ...

  3. 程序员应该是使用git

    我来梳理下我想用git做的事情应该拥有那些功能: 本地的git命令以及图形界面,好让我在没有联网的时候创建git版本控制记录历史功能 一个github账号,好让我可以把本地的git仓库同步到那里 功能 ...

  4. 遍历注册表回调函数(仿PCHunter CmpBack)

    遍历注册表回调函数(仿PCHunter CmpBack) typedef struct _CAPTURE_REGISTRY_MANAGER { PDEVICE_OBJECT deviceObject; ...

  5. LR通过snmp监控linux下的mysql

    LR通过snmp监控linux下的mysql 在linux底下安装配置snmp: 1.使用系统盘安装rpm包(这种方式最好) 2.在www.net-snmp.org处下载net-snmp安装(安装后有 ...

  6. hdu 1573 X问题 不互质的中国剩余定理

    X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  7. 为什么下载APP,扫描二维码,关注微信公众号,就会送牛奶送小礼品?下载使用量高,会怎样?

    以前的老办法是到处贴广告,电视上,广播上各种宣传. 在互联网时代,企业要盈利,除了不断优化升级自己的产品和服务,大量推广宣传产品,还要懂得用户思维.现在有网站,有APP,有微信,有二维码,可以卖产品, ...

  8. css3 -- 文本

    1.坐标轴: 上左为负数 下右为正数 2.text-shadow E{text-shadow:x y color:} 还可以负数实现 E{text-shadow:x y blur-radius col ...

  9. iOS App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure.

    You can easily add it to the plist using the GUI: On the last line add the + Enter the name of the g ...

  10. 【原】iOS学习38网络之数据解析

    1. 解析的基本的概念 解析:从事先规定好的格式中提取数据 解析前提:提前约定好格式,数据提供方按照格式提供数据.数据获取方则按照格式获取数据 iOS开发常见的解析:XML解析.JOSN解析 2. X ...