Longest Substring Without Repeating Characters解题报告

使用双指针,i遍历全部字符,start收集重复的次数,最后不重复出现的字符个数maxx为i-start+1;

//  main.cpp

//  Longest Substring

//

//  Created by Bowie Hsu  on 14/11/21.

//  Copyright (c) 2014年 Bowie Hsu . All rights reserved.

//

#include <iostream>

#include <string>

#include <vector>

using namespace std;

class Solution {

public:

int lengthOfLongestSubstring(string s) {

int i,maxx=0,start=0;

bool sign[256]={false};

//int position[30];

memset(sign,0,sizeof(sign));

for(i=0;i<s.length();++i)

{

char c=s[i];

if (!sign[c]) {   //第一次出现

sign[c]=true;//记录出现的位置

maxx=maxx>i-start+1?maxx:i-start+1;

}

else             //重复,将start提前

{

//int j=start;

while(s[start] != c)

{

sign[s[start]] = false;

++start;

}

++start;

}

}

return maxx;

}

};

int main()

{

Solution x;

int ans;

ans=x.lengthOfLongestSubstring("wlrbbmqbhcdarzowkkyhiddqscdxrjmowfrxsjybldbefsarcbynecdyggxxpklorellnmpapqfwkhopkmco");

cout<<ans<<endl;

//cout<<"the result is"<<endl;

}

时间: 2024-11-05 13:31:04

Longest Substring Without Repeating Characters解题报告的相关文章

leetcode : Longest Substring Without Repeating Characters 解题报告

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.

【LeetCode】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

LeetCode解题笔记 - 3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.

LeetCode解题思路:3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.

Java for LeetCode 003 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 subst

Leetcode Longest Substring Without Repeating Characters python

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 subst

周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)

这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which t

LeetCode3: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 s

[C++]LeetCode: 105 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 s