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.
 
分析:贪心法,从头扫描到尾,O(n)搞定 用一个map[256]来记录出现过的字符位置。遇到没有出现过的字符,将map对应位置标记,并且子串长度+1;遇到出现过的字符,将子串自上一次出现该字符位置前面的截断,对这部分截断的字符,标记map为notFound,重新计算子串长度
参考: http://blog.unieagle.net/2012/09/30/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Alongest-substring-without-repeating-characters/
class Solution {
public:
int lengthOfLongestSubstring(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int map[] ; for(int i = ; i< ; i++)
map[i] = -; const int Notfound = -;
int firstPos = , lastPos = ,len = ;
int i, j;
int maxLen = ; for(i = ; i < s.size(); i++ )
{ int value = map[s[i]] ; if(value == Notfound)
{
len++;
map[s[i]] = i;
maxLen = maxLen > len ? maxLen : len;
}else{ lastPos = value;
for(j = firstPos ; j < lastPos ; j++)
map[s[j]] = Notfound ; firstPos = lastPos + ;
len = i - firstPos + ;
map[s[i]] = i;
}
} return maxLen ; }
};

 

LeetCode_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. subTree

    struct Tree() { int val; Tree *left, *right; Tree(int a): val(a), left(NULL), right(NULL){} } bool h ...

  2. 终于懂了:Delphi的函数名不是地址,取地址必须遵守Object Pascal的语法(Delphi和C的类比:指针、字符串、函数指针、内存分配等)good

    这点是与C语言不一样的地方,以前我一直都没有明白这一点,所以总是不明白:函数地址再取地址算怎么回事? ------------------------------------------------- ...

  3. 一些pyhon的学习资料

    一直没有时间学习python,都说python语法简洁优美.但是我看它的语法还是不爽啊,没C/C++好阅读.但是C/C++又必须了解底层的计算机模型.着实会门槛高一些.特别是C++,对于我来说简直就是 ...

  4. joomla学习网站资料

    http://joomlaol.com/joomla-chinese/joomla-study/340-joomla-skills.html joomla的强大相信学过和用过joomla的童鞋都知道吧 ...

  5. 关于ognl.OgnlException: target is null for setProperty(null的解决方案

    在跑struts2的时候有时候会出现上面的错,特别是新手, 这种情况是在struts2高级的POJO访问时候出现的s 警告: Error setting expression 'user.passwo ...

  6. CentOS 6.3中安装OpenCV2.3.1

    下面为自己测试可用的OpenCV在Linux下的安装步骤 .检查并安装相关程序,确保gtk安装成功,否则无法显示图片 yum install gcc-c++ yuminstall gtk-devel. ...

  7. App上线流程全攻略(续)-iOS8之后的改动与所遇日常错误

    随着iOS8的公布,iTunes Connect的界面也是发生了非常大的改变,App 上传到 Store上面的步骤也是发生了些改变.以下继续用图说话: /*********************** ...

  8. c语言学习之结构篇代码演示样例-输入n个同学的姓名,数学英语成绩,依照平均分从低到高排序并输出

    #include<stdio.h> void main(){ const int count = 5;//定义数量 struct student{ char name[80]; float ...

  9. _js day9

  10. Mybatis 插入操作时获取主键 (Oracle 触发器与SEQ)

    1.通过Oracle序列 -- Create sequence create sequence SEQ_DW_EWSYSTEM minvalue 1 maxvalue 9999999999999999 ...