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.

题解:

使用贪心策略,Hash记录字符上一次出现的位置,并记录起点。

每次更新的时候,记得将start checkpoint 之间出现的字符location清空,以免重复。

 class Solution {
public:
int lengthOfLongestSubstring(string s) {
int location[],i,index,j;
for(i=;i<;i++) location[i]=-;
int start=,maxLength=,templ=;
for(i=;i<s.size();i++)
{
index = s[i];
if(location[index]== -)
templ++;
else
{
if(templ>maxLength)
{
maxLength = templ;
// cout<<s.substr(start,templ)<<endl;
}
for(j=start;j<location[index];j++)
location[s[j]]=-;
templ =templ - (location[index]-start);
start = location[index]+;
}
location[index]=i;
}
if(i==s.size() && templ>maxLength) maxLength=templ;
return maxLength;
}
};

转载请注明出处: http://www.cnblogs.com/double-win/

[LeetCode 题解]: Longest Substring Without Repeating Characters的更多相关文章

  1. LeetCode题解 || Longest Substring Without Repeating Characters (O(n)算法)问题

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

  2. LeetCode题解——Longest Substring Without Repeating Characters

    题目: 给定一个字符串,返回其中不包含重复字符的最长子串长度. 解法: 维持两个指针,第一个指向子串开始,第二个负责遍历,当遍历到的字符出现在子串内时,应计算当前子串长度,并更新最长值:然后第一个指针 ...

  3. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...

  4. [Leetcode Week1]Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...

  5. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  6. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  7. [LeetCode] 3. Longest Substring Without Repeating Characters 解题思路

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

  8. [LeetCode][Python]Longest Substring Without Repeating Characters

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  9. LeetCode之Longest Substring Without Repeating Characters

    [题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...

随机推荐

  1. Python实践练习:在 Wiki 标记中添加无序列表

    题目描述 项目:在 Wiki 标记中添加无序列表 在编辑一篇维基百科的文章时,你可以创建一个无序列表,即让每个列表项占据一行,并在前面放置一个星号.但是假设你有一个非常大的列表,希望添加前面的星号.你 ...

  2. C# Graphics中有关绘图质量的几个Mode

    一.CompositingMode 获取一个值,该值指定如何将合成图像绘制到此 Graphics.复合模式确定从源映像的像素是覆盖(SourceCopy)还是组合(SourceOver, 需要使用半透 ...

  3. ZTree 获取选中的项,jQuery radio的取值与赋值

    $("input[name='Sex']:checked").val();//取值 $("input[name='radioName'][value=2]"). ...

  4. vs2012 未找到与约束 ContractName Microsoft.VisualStudio.Utilities.IContentTy...

    错误的大致内容,不能加载某个文件,需要修改web.config文件或者重命名新建.... 周末360大神帮我修复了一下电脑,然而,这一修复导致周一早上的一连串状况, 以上就是错误本尊,刚开始以为同事提 ...

  5. nyoj27-水池数目 (求连通块数目)【dfs】

    http://acm.nyist.net/JudgeOnline/problem.php?pid=27 水池数目 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 南阳 ...

  6. hdu2516-取石子游戏 (斐波那契博弈)【博弈 二分查找】

    http://acm.hdu.edu.cn/showproblem.php?pid=2516 取石子游戏 Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  7. 61. Rotate List(List)

    Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...

  8. 基于HTML5的RDP访问实战

    基于HTML5的RDP访问实战 1.安装guacamole   2.下载源码   3.安装服务端 安装报错 错误   参考 http://www.remotespark.com/html5.html ...

  9. Solidity oraclize解析Json格式数据

    solidity虽然不能解析json数据但是oraclize_query可以直接处理: pragma solidity ^; import "github.com/oraclize/ethe ...

  10. #error用法

    #error命令是C/C++语言的预处理命令之一,当预处理器预处理到#error命令时将停止编译并输出用户自定义的错误消息. 语法: #error [用户自定义的错误消息] 注:上述语法成份中的方括号 ...