LeetCode(3)Longest Substring Without Repeating Characters
题目:
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 hash_tab[256]; //保存当前字符上一次出现的下标
memset(hash_tab , -1 , sizeof(hash_tab)); //hash_tab中初始化所有值为-1
int max_len = 0, pos = -1;//max_len即是最长子串长度 , pos为当前子串的开始位置 for(int i=0 ; i<s.length() ; i++)
{
//每个字符的元位置初始化为-1 ,当当前字符是重复字符时,改变子串的开始位置
if(hash_tab[s[i]] > pos)
{
pos = hash_tab[s[i]];
}//if if(i-pos > max_len)
{
max_len = i-pos;
}//if //更改首次出现字符的位置
hash_tab[s[i]] = i;
}//for
return max_len;
}
};
LeetCode(3)Longest Substring Without Repeating Characters的更多相关文章
- LeetCode 第 3 题(Longest Substring Without Repeating Characters)
LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...
- LeetCode第三题—— Longest Substring Without Repeating Characters(最长无重复子字符串)
题目描述 Given a string, find the length of the longest substring without repeating characters. Example ...
- Leetcode经典试题:Longest Substring Without Repeating Characters解析
题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...
- 【LeetCode】3 、Longest Substring Without Repeating Characters
题目等级:Medium 题目描述: Given a string, find the length of the longest substring without repeating chara ...
- leetcode第三题--Longest Substring Without Repeating Characters
Problem:Given a string, find the length of the longest substring without repeating characters. For e ...
- leetcode第三题Longest Substring Without Repeating Characters java
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- LeetCode解题笔记 - 3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- LeetCode Hash Table 3. Longest Substring Without Repeating Characters
HashMap的应用可以提高查找的速度,键key,值value的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决.而本题需要解决的问题就是判断新遍历到的字 ...
- LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)
题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...
随机推荐
- Tourists Codeforces - 487E
https://codeforces.com/contest/487/problem/E http://uoj.ac/problem/30 显然割点走过去就走不回来了...可以看出题目跟点双有关 有一 ...
- 1047 - Best couple 好题~
http://www.ifrog.cc/acm/problem/1047 思路很简单,跑一发floyd,然后再用km. 但是问题来了,这个有可能n != m.那怎么办? 其实可以补上一些不存在的点.来 ...
- 响应式Spring Cloud初探
响应式Spring Cloud初探 分类:工程原文链接:The Road to Reactive Spring Cloud作者: JOSH LONG译者: helloworldtang日期:JUNE ...
- windows 服务器开设端口
主要用于服务器建设网站的时候开设端口 依次点击“开始”—“控制面板”—“windows防火墙” 2 先点击“打开或关闭windows防火墙”将windows防火墙打开 3 点击“高级设置” 4 设置入 ...
- go实现生产者消费者
package main import ( "fmt" "math/rand" ) func main() { ch := make(chan int) don ...
- .NET Core 1.0 CentOS7 尝试(一、安装)
昨天宣布 ASP.NET Core RC2,据说差不多稳定了,以后不会有大改了. 参考:https://blogs.msdn.microsoft.com/webdev/2016/05/16/annou ...
- CSS元素隐藏的display和visibility
一.CSS元素隐藏 在CSS中,让元素隐藏(指屏幕范围内肉眼不可见)的方法很多,有的占据空间,有的不占据空间:有的可以响应点击,有的不能响应点击. { display: none; /* 不占据空间, ...
- easyui 刷新页面
window.location.reload()刷新当前页面. parent.location.reload()刷新父亲对象(用于框架) opener.location.reload()刷新父窗口对象 ...
- LintCode 30插入区间
问题 给出一个无重叠的按照区间起始端点排序的区间列表. 在列表中插入一个新的区间,你要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间). 样例 插入区间[2, 5] 到 [[1,2], ...
- UVA 10003 cuting sticks 切木棍 (区间dp)
区间dp,切割dp[i][j]的花费和切法无关(无后效性) dp[i][j]表示区间i,j的花费,于是只要枚举切割方法就行了,区间就划分成更小的区间了.O(n^3) 四边形不等式尚待学习 #inclu ...