题目:

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) {
map<char, int> visited;
for ( char c='a'; c<='z'; ++c ) visited[c]=-;
int begin = ;
int longest_substring = ;
for ( int i = ; i < s.size(); ++i )
{
char curr = s[i];
if ( visited[curr]>=begin )
{
longest_substring = std::max(longest_substring, i-begin);
begin = visited[curr]+;
}
visited[curr]=i;
}
return std::max((int)s.size()-begin, longest_substring);
}
};

tips:

Greedy解法,主要维护以下两个内容。

维护一个哈希表:<字母,上一次字母出现的位置>

维护一个begin:表示不重复字符串的起始位置

如果当前字母在之前出现过,且出现的位置大于等于begin,则证明遇到了重复的字符;更新begin的位置为该字母上一次出现的位置+1。

在整个过程中,每次遇到不重复的字符时,就往后走。

这里要注意边界条件,即最长的字符串包含最后s的最后一个字符。

则在推出循环后,要再更新一次longest_substring。

===============================================

第二次过这道题,还是用hashmap的思路,第一次超时了,原因是想删除在local长度之前的那些hashmap表中的字符。

第二次修改了一下,hashmap表中的字符即使有重复的也不要紧,只要不在local长度范围内,都可以接受继续往后走,然后更新字符最新的位置就可以了。

class Solution {
public:
int lengthOfLongestSubstring(string s) {
map<char, int> charPosition;
int global = ;
int local = ;
for ( int i=; i<s.size(); ++i )
{
if ( charPosition.find(s[i])==charPosition.end() || charPosition[s[i]]<i-local )
{
charPosition[s[i]] = i;
local++;
}
else
{
global = max(global, local);
local = i - charPosition[s[i]];
charPosition[s[i]] = i;
}
}
return max(global, local);
}
};

【Longest Substring Without Repeating Characters】cpp的更多相关文章

  1. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

  2. 【LeetCode】Longest Substring Without Repeating Characters 解题报告

    [题意] Given a string, find the length of the longest substring without repeating characters. For exam ...

  3. 【leetcode】Longest Substring Without Repeating Characters

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

  4. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

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

  5. 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串

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

  6. 【leetcode】Longest Substring Without Repeating Characters (middle)

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

  7. 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters

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

  8. 【LeetCode】3. Longest Substring Without Repeating Characters

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

  9. 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters

    一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...

随机推荐

  1. 解决mysql连接输入密码提示Warning: Using a password on the command line interface can be insecure

    有时候客户端连接mysql需要指定密码时(如用zabbix监控mysql)5.6后数据库会给出个警告信息 mysql -uroot -pxxxx Warning: Using a password o ...

  2. 微信jssdk实现分享到微信

    本来用的是这个插件http://overtrue.me/share.js/和百度分享 相同之处:在微信分享的时候,分享的链接都不能获取到缩略图... 不同之处:百度分享在微信低版本是可以看到缩略图的( ...

  3. C++中的异常安全性

    http://blog.csdn.net/bonchoix/article/details/8046727 一个函数如果说是“异常安全”的,必须同时满足以下两个条件:1.不泄漏任何资源:2.不允许破坏 ...

  4. SSM框架快速搭建

    1.   新建Maven项目 ssm 2.    pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xml ...

  5. 小弟在研究CUDA时出现一个问题,求解

    这是<GPU高性能编程CUDA中文实战>中的例子,第七章,热传导模拟,但是出现下面的问题,求牛人解读.小弟跪谢... 主要问题就是关键字变白. 但是添加需要的头文件后一些系统自带的关键字也 ...

  6. Hadoop集群批量命令执行

    ./pdsh -R ssh -w node-10-0[0-5] hostname -R:指定传输方式,默认为rsh,本例为ssh,如果希望ssh传输需要另行安装pdsh-rcmd-ssh,如果希望ss ...

  7. java程序换图标

    ImageIcon img = new ImageIcon("D:\\mahou-in-action\\ShiJuanFenXi\\src\\zoom-in.png"); inst ...

  8. Mac 系统 + Chrome浏览器 网页前端出现中文文字反转或顺序错乱

    问题背景 React开发的系统,收到一个BUG反馈,*"号个人统计"文字不正确,应为"个人号统计"*. 收到BUG后,打开浏览器查验是什么情况,难道犯了最基本的 ...

  9. 1305: [CQOI2009]dance跳舞

    Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 4169  Solved: 1804[Submit][Status][Discuss] Descripti ...

  10. mysql运维-二进制日志BINARY LOG清理

       1.1 方法1:PURGE MASTER LOGS     语法: PURGE { BINARY | MASTER } LOGS { TO 'log_name' | BEFORE datetim ...