LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int locs[];//保存字符上一次出现的位置
memset(locs, -, sizeof(locs)); int idx = -, max = ;//idx为当前子串的开始位置-1
for (int i = ; i < s.size(); i++)
{
if (locs[s[i]] > idx)//如果当前字符出现过,那么当前子串的起始位置为这个字符上一次出现的位置+1
{
idx = locs[s[i]];
} if (i - idx > max)
{
max = i - idx;
} locs[s[i]] = i;
}
return max;
}
};
其他解题方法:
#include <bits/stdc++.h>
#define MAX 110 using namespace std; int pos[MAX], exist[MAX]; int main()
{
string s;
int lens,i,j,start,max_num;
while(cin>>s)
{
lens=s.size();
max_num=,start=;
memset(pos,,sizeof(pos));
memset(exist,,sizeof(exist));
for(i=;i<lens;i++)
{
if(exist[s[i]-'a'])
{
for(j=start;j<=pos[s[i]-'a'];j++)
exist[s[j]-'a']=;
start=pos[s[i]-'a']+;
exist[s[i]-'a']=;
pos[s[i]-'a']=i;
}
else
{
exist[s[i]-'a']=;
pos[s[i]-'a']=i;
max_num=max(max_num,i-start+);
}
}
printf("%d\n",max_num);
}
}
LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)的更多相关文章
- [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] 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. 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 最长不重复子串
题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离 ...
- [LeetCode] Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 003 Longest Substring Without Repeating Characters 最长不重复子串
Given a string, find the length of the longest substring without repeating characters.Examples:Given ...
随机推荐
- How can I exclude directories from grep -R?
‘--exclude-dir=dir’ Exclude directories matching the pattern dir from recursive directory searches. ...
- Python单元测试框架之pytest---如何执行测试用例
介绍 pytest是一个成熟的全功能的Python测试工具,可以帮助你写出更好的程序. 适合从简单的单元到复杂的功能测试 l 模块化parametrizeable装置(在2.3,持续改进) l 参 ...
- 自动化基础普及之selenium是啥?
Selenium 并不像QTP那样让人一下子就明白是什么?它是编程人员的最爱,但它却对测试新手产生了很大的阻碍. Selenium 是啥? Selenium RC是啥? Webdriver 又是啥? ...
- 妙味5:document.cookie 操作
本地环境中测试需要用fireFox,其它几个浏览器不行,服务器都可以测出正确结果 cookie特点: 1. 如登陆信息存储,同一论坛打开多个页面不用重复登陆,就是通过cookie来存取实现: 2. ...
- xss-跨站脚本攻击-后台传给前端的html标签安全显示
作用 后台拼接的html字符串传到前端,默认是不安全的,需要告诉前端这个字符串是安全的,可以正常显示html标签. 知识点 1.定义 2 3 <script> 获取session ...
- 反射动态创建不同的Processor
1. 定义抽象方法 public abstract class BaseProcesser { public abstract void GetCustomerReportCard ...
- 数论 - 素数的运用 --- poj 2689 : Prime Distance
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12512 Accepted: 3340 D ...
- 将HTML5 Canvas的内容保存为图片
主要思想是借助Canvas自己的API - toDataURL()来实现,整个实现 HTML + JavaScript的代码很简单. 代码如下: <html> <meta http- ...
- Ajax,谷歌提示AutoCompleteExtender控件
提示内容从数据库中读取: ------------------------------------------页面 <asp:ScriptManager ID="ScriptManag ...
- mongodb学习1---基本命令
0:基本命令:1,登录mongodb数据库mongo 2,查看数据库,选择数据库show dbs;use table1; 3,查看集合show collections; 4,查看集合所有数据db.集合 ...