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. 19_java之List和Set

    01List接口的特点 A:List接口的特点: a:它是一个元素存取有序的集合. 例如,存元素的顺序是11.22.33.那么集合中,元素的存储就是按照11.22.33的顺序完成的). b:它是一 ...

  2. Python小知识点(3)--装饰器

    (1)装饰器含参数,被装饰函数不含(含)参数 实例代码如下: import time # 装饰器函数 def wrapper(func): def done(*args,**kwargs): star ...

  3. mybatis 传入集合参数遍历 查询总结

    出自:http://blog.csdn.net/u013628152/article/details/51184641 1. findByIds(List ids) 如果参数的类型是List, 则在使 ...

  4. Bootstrap 与 Jquery validate 结合使用——简单实现

    首先必须引入的JS和CSS <script type="text/javascript" src="${ctx}/static/js/jquery-1.9.1.mi ...

  5. malloc()与calloc区别 (转)

    另外说明: 1.分配内存空间函数malloc 调用形式: (类型说明符*) malloc (size) 功能:在内存的动态存储区中分配一块长度为"size" 字节的连续区域.函数的 ...

  6. HTTP 1.0 & 1.1

    简介: HTTP ( HyperText Transfer Protocol ) 超文本传输协议,是互联网上应用最广泛的一种网络协议. HTTP 是一个客户端和服务端请求.应答的标准.使用 WEB 浏 ...

  7. SpringMVC前后台数据传递中Json格式的相互转换(前台显示格式、Json-lib日期处理)及Spring中的WebDataBinder浅析

    两个方向: 一.前台至后台: Spring可以自动封装Bean,也就是说可以前台通过SpringMVC传递过来的属性值会自动对应到对象中的属性并封装成javaBean,但是只能是基本数据类型(int, ...

  8. Synchronized块同步变量的误区

    我们可以通过synchronized块来同步特定的静态或非静态方法.要想实现这种需求必须为这些特性的方法定义一个类变量,然后将这些方法的代码用synchronized块括起来,并将这个类变量作为参数传 ...

  9. Scala基础:模式匹配和样例类

    模式匹配 package com.zy.scala import scala.util.Random /** * 模式匹配 */ object CaseDemo { def main(args: Ar ...

  10. 刷题向》关于一道比较优秀的递推型DP(openjudge9275)(EASY+)

    先甩出传送门:http://noi.openjudge.cn/ch0206/9275/ 这道题比较经典, 最好不要看题解!!!!! 当然,如果你执意要看我也没有办法 首先,显然的我们可以用 f [ i ...