LeetCode OJ: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.
使用双指针,前一个指针向前走,有不相同的字符的时候标记即可,发现相同时停止,这是后面的字符向后走,直到发现这个字符,然后前指针继续向前走,一直重复直到整个过程的结束,代码如下:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if(!s.size()) return ;
int i = , j = ;
int maxSubLen = ;
bool canUse[]; //char类型只有256个
memset(canUse, true, sizeof(canUse));
canUse[s[]] = false;
while(j < s.size()){
if(!canUse[s[j]]){
maxSubLen = max(maxSubLen, j - i);
while(i < j){
if(s[i] != s[j])
canUse[s[i]] = true, ++i;
else{
i++;
break;
}
}
}else{
maxSubLen = max(maxSubLen, j - i + );
canUse[s[j]] = false;
}
++j;
}
return maxSubLen;
}
};
写的有点乱啊,见谅见谅。。
LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)的更多相关文章
- 3. Longest Substring Without Repeating Characters - 最长无重复字符子串-Medium
Examples: Description: Given a string, find the length of the longest substring without repeating ch ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- leetcode 3 Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)
Given a string, find the length of the longest substring without repeating characters. Example 1: ...
- [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(最长不重复连续子串Medium)
题目意思:求字符串中,最长不重复连续子串 思路:使用hashmap,发现unordered_map会比map快,设置一个起始位置,计算长度时,去减起始位置的值 eg:a,b,c,d,e,c,b,a,e ...
随机推荐
- 解决“检测到有潜在危险的Request.Form值”
先看如下 web.config 的代码: <system.web> <compilation debug="true" targetFramework=& ...
- hadoop https配置
在 hadoop1生成ca并拷贝至hadoop2,hadoop2. (密码随便设置,大于6位即可.如adminadmin) cd /etc/https openssl req -new -x509 - ...
- 20145302张薇《Java程序设计》第七周学习总结
20145302 <Java程序设计>第七周学习总结 教材学习内容总结 第十三章 时间的度量 Greenwich Mean Time,格林威治时间,简称GMT时间,由观察太阳而得来: Un ...
- 20145327实验二Java面向对象程序设计
Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 伪代码,产品代码,测试代码的应用 ...
- linux内核第一二章总结
1 Linux内核简介 1 Unix的历史 1.Unix演化版实现了任务管理.换页机制.TCP/IP等新的特性. 2.Unix的特点: Unix很简洁,仅仅提供几百个系统调用并且有一个非常明确的设计目 ...
- Fiddler4工具配置及调试手机和PC端浏览器
Fiddler最大的用处: 模拟请求.修改请求.手机应用调试 Fiddler最新版本 下载地址: http://www.telerik.com/download/fiddler Fiddler 想要监 ...
- excel日期插件
效果图 Private Sub DTPicker1_Click() ActiveCell.Value = DTPicker1.Value DTPicker1.Visible = False End S ...
- localAddress
$(function(){ <% out.println("/** ip:"+request.getLocalAddr()+"("+request.get ...
- vROPS中获取虚拟机在VC中的UUID
vROPS中虚拟机对象的ID为resourceID,跟vCenter中虚拟机的UUID是不一致的,因此想要将vROPS中的虚拟机和vCenter中的虚拟机对应肯定不能靠虚拟机名称,而是一定要靠UUID ...
- RabbitMQ Network Partitions
Clustering and Network Partitions RabbitMQ clusters do not tolerate network partitions well. If you ...