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 ...
随机推荐
- Hadoop第6周练习—在Eclipse中安装Hadoop插件及测试(Linux操作系统)
1 运行环境说明 1.1 硬软件环境 1.2 机器网络环境 2 :安装Eclipse并测试 2.1 内容 2.2 实现过程 2.2.1 2.2.2 ...
- left join on
问题: select * from A left join f on e.cust=f.account_id where f.status='0' 与 select * from A left jo ...
- iOS-定时器
一.定时器的作用 在软件开发过程中,我们常常需要在某个时间后执行某个方法,或者是按照某个周期一直执行某个方法.在这个时候,我们就需要用到定时器. 二.定时器的种类 大概有三种方法:NSTimer.CA ...
- Java 8的新并行API - 魅力与炫目背后
这是一篇译文,原文链接见这里. 本文同时发表在ImportNew上,转载请注明出处. 我很擅长同时处理多项任务.就算是在写这篇博客的此刻,我仍然在为昨天在聚会上发表了一个让大家都感到诧异的评论而觉得尴 ...
- c# XML序列化与反序列化
c# XML序列化与反序列化 原先一直用BinaryFormatter来序列化挺好,可是最近发现在WinCE下是没有办法进行BinaryFormatter操作,很不爽,只能改成了BinaryWrite ...
- [阅读]个人阅读作业week7
People-oriented in Agile People-oriented in Agile One Leader Prepare Good ideas from users People-or ...
- oracle 查询 当前最大时间的value的值
数据列表: table : text id datetime name value 1 2015-03-1 张三 3400 2 2015-03-1 ...
- sql server2008中左连接,右连接,等值连接的区别
数据库中的连接我了解到有left join,right join,inner join这些,以下是它们的区别: 1)左连接(left join):先取出a表的所有数据,再取出a.b表相匹配的数据 2) ...
- php获取textarea的值并处理回车换行的方法
//注:\n是用双引号包的的,双引号!!双引号!!! explode("\n",$row[0]['value']
- jquery checkbox checked
1.question: when first operate the checkbox with sentence like : $("input[type=checkbox]") ...