LeetCode3:Longest Substring Without Repeating Characters
题目:
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的更多相关文章
- No.003:Longest Substring Without Repeating Characters
问题: Given a string, find the length of the longest substring without repeating characters.Example:Gi ...
- Leetcode经典试题:Longest Substring Without Repeating Characters解析
题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...
- LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List
题目中文:没有重复字符的最长子串 题目难度:Medium 题目内容: Given a string, find the length of the longest substring without ...
- leetcode笔记:Longest Substring Without Repeating Characters
一. 题目描写叙述 Given a string, find the length of the longest substring without repeating characters. For ...
- Q3:Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters 官方的链接:3. Longest Substring Without Repeating Chara ...
- leetcode:Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【LeetCode3】Longest Substring Without Repeating Characters★★
题目描述: 解题思路: 借用网上大神的思想:the basic idea is, keep a hashmap which stores the characters in string as key ...
- Leetcode3:Longest Substring Without Repeating Characters@Python
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
随机推荐
- WAMPServer 默认安装启动后,图标显示橙黄色
说明:我是在家的电脑上面学习的WAMPServer的,而家的电脑很干净.这次来学校以后,实验室的电脑被我经常的折磨,今天在安装以后,启动后,发现图标有红色到黄色,就停止了.其实,问题的根源很简单,某个 ...
- 桃小蛋 简单粗暴BFC总结—附代码图和效果图
说明:问题图与解决图的区别:黄色箭头那行代码的有和无. BFC 定义 BFC(Block formatting context)直译为"块级格式化上下文".它是一个独立的渲染区域, ...
- Atitit 图像处理30大经典算法attilax总结
Atitit 图像处理30大经典算法attilax总结 1. 识别模糊图片算法2 2. 相似度识别算法(ahash,phash,dhash)2 3. 分辨率太小图片2 4. 横条薯条广告2 5. 图像 ...
- WPF入门教程系列十一——依赖属性(一)
一.依赖属性基本介绍 本篇开始学习WPF的另一个重要内容依赖属性. 大家都知道WPF带来了很多新的特性,其中一个就是引入了一种新的属性机制——依赖属性.依赖属性出现的目的是用来实现WPF中的样式.自动 ...
- bootstrap实现嵌入的button
bootstrap实现嵌入的button 如下的效果: <div class="form-inline"> <div class="input-grou ...
- Java environment variables and their functionality
Explanations of Functionalities: 1. PATH env variable It is used to search the command directory whe ...
- 部署到IIS报错:HTTP错误500.19,错误代码0x800700d
title=部署到IIS报错:HTTP错误500.19,错误代码0x800700d. 用vs直接运行网站没问题,部署到IIS就报错,由此可知应该是IIS中不支持网站相关配置. 查找发现在web.c ...
- iOS苹果企业证书被撤销以及启用与管理
在国内, 积分墙以及各大助手(爱思助手, 91苹果助手, XY苹果助手, PP助手, 快用助手)等业务领域都在使用,苹果对证书的使用越来越严格.简单的分析一下,证书被封的原因. 一般证书被封会收到 ...
- 简单使用AutoMapper实现DTO转换
DTO(Data Transfer Object)数据传输对象,只是传输数据,完成与领域对象之间的转换,并不包含领域业务处理. 当领域模型设计人员只关注核心业务,满足于领域模型的精巧,而不关心具体实现 ...
- MySQL的用户和权限介绍
一.关于MySQL权限的几点常识: 1.MySQL的权限系统主要用来验证用户的操作权限. 2.在MySQL内部,权限信息存放在MySQL数据库的granttable里.当mysql启动后,grantt ...