Description:

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.

给定一个字符串,求字符串中最长不重复的字串长度。

又是一道没有数据范围的题目,这里字符串长可以超过3w,所以暴力不可搞。最后用很巧妙的运用起点和重点位置的 顺序选择,和对字符先后出现位置的统计,来求得结果。

class Solution {

public:

int lengthOfLongestSubstring(string s) {

int maxLen = 0, start = -1;

map<char, int>mapChar;

for (int i = 0; s[i]; i ++) {

if (mapChar.count(s[i])) {//这里学了一招,map的count方法可以统计对应类型出现的次数,不用再dt的初始化了

start = max(start, mapChar[s[i]]);

}

mapChar[s[i]] = i;

maxLen = max(maxLen, i - start);

}

return maxLen;

}

};

Leetcode03---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 examp ...

  2. LeetCode[3] 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 example, ...

  4. Longest Substring Without Repeating Characters

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

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

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

  6. 【leetcode】Longest Substring Without Repeating Characters

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

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

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

  8. leetcode: longest substring without repeating characters

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

  9. [LeetCode_3] Longest Substring Without Repeating Characters

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

  10. Longest Substring Without Repeating Characters (c#)

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

随机推荐

  1. 关于datanode多磁盘存储策略

    目的: 节点内各存储磁盘均衡 相关参数: dfs.datanode.fsdataset.volume.choosing.policy=org.apache.hadoop.hdfs.server.dat ...

  2. Netty学习总结(4)——图解Netty之Pipeline、channel、Context之间的数据流向

    以下所绘制图形均基于Netty4.0.28版本. 一.connect(outbound类型事件) 当用户调用channel的connect时,会发起一个outbound类型的事件,该事件将在pipel ...

  3. HDU 1325 拓扑排序

    根据题目所给的3个不符合情况的条件,一个个判断图是否符合这3个条件即可 1.不能出现内部环,拓扑排序判断 2.不能有超过1个点的入度为0,因为只有一个树根 3.每个点最多一个入度 这里要注意的一点是这 ...

  4. POJ 3630

    Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20894 Accepted: 6532 Descripti ...

  5. Python函数基础---参数、变量

    函数:指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可. def sayhi( ): # 函数名 print('hello world') sayhi( ) # ...

  6. 【BZOJ2342】双倍回文(manacher,并查集)

    题意: 思路:From http://blog.sina.com.cn/s/blog_8d5d2f04010196bh.html 首先我可以看出: (1)我们找到的串的本身也是一个回文串(显然) (2 ...

  7. mybatis中jdbcType的作用和是否必须

    1. mybatis中 jdbcType 时间类型 当jdbcType = DATE 时, 只传入了 年月日 jdbcType = TIMESTAMP ,  年月日+ 时分秒 2. jdbcType ...

  8. OC-内存管理的一些要点

    创建一个BOOK对象,对其属性进行声明 定义. @property 属性声明 定义了对属性的赋值 -(void) dealloc 方法在对象销毁的时候进行调用. #import <Foundat ...

  9. sgu101Domino

    给你一些边,假设存在欧拉路径就打出来 我的代码例如以下: #include<iostream> #include<cstring> using namespace std; i ...

  10. Python基础--webbrowser

    非常多人,一提到Python,想到的就是爬虫.我会一步一步的教你怎样爬出某个站点. 今天就先介绍一下webbrowser,这个词您肯定不会陌生.对,就是浏览器. 看看Python中对webbrowse ...