题目:

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.

解题思路:

一开始,没看清题目要求,以为是去重,一提交出错才知不是。

只要谈到去重或者无重复之类的词,就应该想到哈希表或bitmap。

这里直接引用网上大牛的解题方法:http://blog.csdn.net/kenden23/article/details/16839757

利用两指针i,j。i走在前,每次到哈希表中查找之前是否出现,没有则将对应位置置1,若出现过,说明开始下一个子串

实现代码:

#include <iostream>
#include <string>
#include <cstring>
using namespace std; /**
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) {
if(s.empty())
return 0;
int len = s.size();
int bitmap[256] = {0};
//memset(bitmap, 0, sizeof(int) * 26);
int maxLen = 0;
int j = 0;
for(int i = 0; i < len; i++)
{
if(bitmap[s[i]] == 0)
{
bitmap[s[i]] = 1;
}
else
{
maxLen = max(maxLen, i-j);
while(s[i] != s[j])//对bitmap进行设置,将重复元素之前的所有元素对应的位置0,因为这些元素不参与下一次字符串
{
bitmap[s[j]] = 0;
j++; }
j++;
}
}
maxLen = max(maxLen, len-j);//for循环退出是因为i =len,所以这里还要判断
return maxLen; }
}; int main(void)
{ string s("wlrbbmqbhcdarzowkkyhiddqscdxrjmowfrxsjybldbefsarcbynecdyggxxpklorellnmpapqfwkhopkmco");
Solution solution;
int res = solution.lengthOfLongestSubstring(s);
cout<<res<<endl;
return 0;
}

LeetCode3:Longest Substring Without Repeating Characters的更多相关文章

  1. No.003:Longest Substring Without Repeating Characters

    问题: Given a string, find the length of the longest substring without repeating characters.Example:Gi ...

  2. Leetcode经典试题:Longest Substring Without Repeating Characters解析

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

  3. LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)

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

  4. LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List

    题目中文:没有重复字符的最长子串 题目难度:Medium 题目内容: Given a string, find the length of the longest substring without ...

  5. leetcode笔记:Longest Substring Without Repeating Characters

    一. 题目描写叙述 Given a string, find the length of the longest substring without repeating characters. For ...

  6. Q3:Longest Substring Without Repeating Characters

    3. Longest Substring Without Repeating Characters 官方的链接:3. Longest Substring Without Repeating Chara ...

  7. leetcode:Longest Substring Without Repeating Characters

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

  8. 【LeetCode3】Longest Substring Without Repeating Characters★★

    题目描述: 解题思路: 借用网上大神的思想:the basic idea is, keep a hashmap which stores the characters in string as key ...

  9. Leetcode3:Longest Substring Without Repeating Characters@Python

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

随机推荐

  1. 三天学会HTML5 之第一天

    引言 HTML5 一直是非常热门的话题,因此此系列文章主要从一些基本功能开始讲起,逐步深入了解HTML5的新概念. 首先了解一些基本的术语和概念. SGML, HTML,XML三者之间的区别 Doc类 ...

  2. 使用Sublime Text 2 编辑Markdown

    http://www.ituring.com.cn/article/6815 一.安装 下载Sublime Text 2 安装 二.安装Package Control 按Ctrl + ` 打开cons ...

  3. React(三)组件的生命周期

    Component Specs and LifeCycle <div id="app"></div> <script src="bower_ ...

  4. 【WP开发】认清“不透明度”与“可见性”的区别

    这两种情况,许多朋友平时都没有注意到: 1.设置Opacity属性的值为0: 2.将Visibility属性设置为Collapsed. 不少人会简单地认为这两种情况是一样的,都是让UI元素看不见. 我 ...

  5. Dijkstra算法(一)之 C语言详解

    本章介绍迪杰斯特拉算法.和以往一样,本文会先对迪杰斯特拉算法的理论论知识进行介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现. 目录 1. 迪杰斯特拉算法介绍 2. 迪杰斯特拉算法 ...

  6. [转]Linux df 命令不更新磁盘数据空间使用情况的解决办法

    当你已经找出并remove掉Linux系统中的大容量文件时,然后使用df -h查看使用情况依旧不变时.可尝试如下方法解决 1.找出那个进程占用了哪些已删除的文件 # 查看哪些被文件还在被哪个进程占用 ...

  7. WEKA使用(基础配置+垃圾邮件过滤+聚类分析+关联挖掘)

    声明: 1)本文由我bitpeach原创撰写,转载时请注明出处,侵权必究. 2)本小实验工作环境为Windows系统下的WEKA,实验内容主要有三部分,第一是分类挖掘(垃圾邮件过滤),第二是聚类分析, ...

  8. $("#id").val()取值textarea是""

    今天取值的时候,判断是null可以通过,证明不是null,明明是空的. 判断是''通过,证明取出来的是''空字符串.

  9. spring学习遇到的问题汇总

    1.spring注解路由方面的误解 我一直以为在web.xml中配置拦截*.action后,在注解路由的时候必须要xxxx.action. 刚才发现,访问的时候xxxx.action,然后@Reque ...

  10. oracle表数据类型number对应java中BIgDecimal转int

    oracle中id为number类型,在java获取id时用getBigDecimal 相匹配, 如果想转换成int,重写model中的getInt方法: public Integer getInt( ...