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 ...
随机推荐
- Communication - 02.Call U
App层 从大拇哥Click CallButton开始手机便已明白,主人这是要打电话.当然,你可以选择直接拨号,也可以通过ContactList,或者从通话记录着手.这些都只是UI的设计不同而已,终归 ...
- Character Controller (角色控制器) 中 Move()和SimpleMove() 的区别
首先给出两者的圣典: CollisionFlagsMove(Vector3motion); Description A more complex move function taking absolu ...
- CSS魔法堂:Position定位详解
一.Position各属性值详解 1. static :默认值,元素将按照正常文档流规则排列. 2. relative :相对定位,元素仍然处于正常文档流当中,但可以通过left.top. ...
- 【第二课】深入理解Handler
简要讲解Handler是做什么的 我们知道,在Android中,app启动会启动一个进程一个线程——UI线程,UI线程是主线程,并且不允许这个线程阻塞超过5秒,一旦超过5秒就会ANR. 所以较为耗时的 ...
- Sprint第三个冲刺(第六天)
一.Sprint介绍 任务进度: 二.Sprint周期 看板: 燃尽图:
- Sprint第三个冲刺(第三天)
一.Sprint介绍 任务进度: 二.Sprint周期 看板: 燃尽图:
- sprint个人总结+读书博客
读书感想: 第8章讲了需求分析,在我的日常软件编写中,肯定需要需求分析的,一个没有需求的软件,编写出来也没有什么意义,只能是丢在一个角落里发霉.需求有各种各样,要怎样才能满足客户的需求呢,那就要 ...
- JavaScript语言知识收藏
接触Web开发也已经有一段时间了,对javascript的认识也比以前有了更加深入的认识了,所以觉得应该整理一下. 一.JavaScript不支持函数(方法)的重载,用一个例子证明如下: functi ...
- 百度地图js根据经纬度定位和拖动定位点
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...
- 批量插入数据 C# SqlBulkCopy使用
转自:http://blog.csdn.net/wangzh300/article/details/7382506 private static void DataTableToSQLServer( ...