题目:

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. java系统性能分析

    netstat -ano | findstr 31900 注意最后是pid 堆栈的作用: 线程死锁分析 辅助CPU过高分析 线程资源不足分析 性能瓶颈分析 关键线程异常退出 Windows:在运行ja ...

  2. Flex小结

    参考两篇文章 文章1 文章2 容器用display: flex;或display: inline-flex;指定为弹性Flex布局.采用Flex布局的元素,称为Flex容器(flex containe ...

  3. 大叔也说Xamarin~Android篇~Activity之间传递数组

    回到目录 我们在开发应用程序时,不可能只使用一个Layout或者一个Activity,比如你个管理系统,要求用户先登陆然后再使用,这时你至少要有两个activity吧,先登陆一个,然后成功后需要跳到别 ...

  4. 360路由器c301最新固件支持万能中继

    360路由器c301现在已经停产了.出厂的固件是360_301_0.7.3.8.这个版本不支持中继.5G信号.本文主要介绍如何刷最新固件以及设置万能中继. 本文为作者原创,发表在博客园:http:// ...

  5. ubuntu下在apache部署python站点

    ubuntu下在apache部署python站点 我的是ubuntu14 32为的虚拟机,默认安装的python为3.4 环境:apache + mysql + django + python3 软件 ...

  6. 第五节:表单标签的用法——value绑定和修饰符

    1.表单标签的用法--value绑定和修饰符 value绑定的写法:v-bind:value 或者简写 :value 修饰符: lazy , Number , trim . 用法如:  v-model ...

  7. CAS原子锁 高效自旋无锁的正确用法

    "atomic_lock.h" #pragma once #ifndef _atomic_lock_h_include_ #define _atomic_lock_h_includ ...

  8. 编译原理:正规式转变成DFA算法

    //将正规式转变成NFApackage hjzgg.formal_ceremony_to_dfa; import java.util.ArrayList; class Edge{ public int ...

  9. EnjoyCSS – 在线的,先进的 CSS3 代码生成器

    EnjoyCSS 是一款先进的 CSS3 代码生成工具,可以让你摆脱日常的编码.它方便和易于使用的用户界面允许您快速,无需编码就可以调节出丰富的图形样式.您将能够玩转所有的 EnjoyCSS 参数,就 ...

  10. $.when(deferreds)

    作者:禅楼望月(http://www.cnblogs.com/yaoyinglong ) 1 引子 上一篇博文中介绍的Deferred,它表示一个延迟对象.但是很多时候,我们需要在多个延迟对象(异步代 ...