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 substring is
"b", with the length of 1.

解题思路:

一开始,没看清题目要求,以为是去重,一提交出错才知不是。

只要谈到去重或者无重复之类的词,就应该想到哈希表或bitmap。

这里直接引用网上大牛的解题方法:http://blog.csdn.net/kenden23/article/details/16839757

利用两指针i,j。i走在前,每次到哈希表中查找之前是否出现,没有则将对应位置置1,若出现过,说明开始下一个子串

实现代码:


#include <iostream>
#include <string>
#include <cstring>
using namespace std;

/**
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) {
if(s.empty())
return 0;
int len = s.size();
int bitmap[256] = {0};
//memset(bitmap, 0, sizeof(int) * 26);
int maxLen = 0;
int j = 0;
for(int i = 0; i < len; i++)
{
if(bitmap[s[i]] == 0)
{
bitmap[s[i]] = 1;
}
else
{
maxLen = max(maxLen, i-j);
while(s[i] != s[j])//对bitmap进行设置,将重复元素之前的所有元素对应的位置0,因为这些元素不参与下一次字符串
{
bitmap[s[j]] = 0;
j++;

}
j++;
}
}
maxLen = max(maxLen, len-j);//for循环退出是因为i =len,所以这里还要判断
return maxLen;

}
};

int main(void)
{

string s("wlrbbmqbhcdarzowkkyhiddqscdxrjmowfrxsjybldbefsarcbynecdyggxxpklorellnmpapqfwkhopkmco");
Solution solution;
int res = solution.lengthOfLongestSubstring(s);
cout<<res<<endl;
return 0;
}

LeetCode3:Longest Substring Without Repeating
Characters,码迷,mamicode.com

LeetCode3:Longest Substring Without Repeating
Characters

时间: 2024-10-13 15:32:27

LeetCode3:Longest Substring Without Repeating Characters的相关文章

LeetCode 03: 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

一. 题目描写叙述 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 lon

LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List

题目中文:没有重复字符的最长子串 题目难度:Medium 题目内容: 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 &q

Leetcode经典试题:Longest Substring Without Repeating Characters解析

题目如下: Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: "bbbbb" Output: 1 E

lintcode 中等题:longest substring without repeating characters 最长无重复字符的子串

题目 最长无重复字符的子串给定一个字符串,请找出其中无重复字符的最长子字符串. 例如,在"abcabcbb"中,其无重复字符的最长子字符串是"abc",其长度为 3. 对于,"bbbbb",其无重复字符的最长子字符串为"b",长度为1. 解题 利用HashMap,map中不存在就一直加入,存在的时候,找到相同字符的位置,情况map,更改下标 public class Solution { /** * @param s: a s

第三题:Longest Substring Without Repeating Characters

题目链接:题目链接 找到最长不重复子串,使用hash方法来做是极好的!遍历string,找到一个字符,然后会判断这个字符是不是已经在当前的这个子串中,这个使用hash方法,因为可见字符最多256个,那么使用hash[256]就可以!初始化为-1,保存的是相应的字符在string中的位置.如果遇到在前面存在的字符,那么需要将这个子串的start位置移动到已经存在的位置后面一位,然后继续遍历,直到遍历结束. 具体看下面的代码: class Solution { public: int lengthO

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

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.

[Swift]LeetCode3. 无重复字符的最长子串 | 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.