刷题3. Longest Substring Without Repeating Characters

一、题目

Longest Substring Without Repeating Characters,具体请自行搜索。

这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂。

但要做到bug free有点难度,主要是边界的问题。

二、这个题目,我自己实现,没有参考代码

提交了5次:

第1次: Wrong Answer,主要是“ ”这个空串不对

第2次、第3次:Runtime Error,"au" out of Range

第4次:Wrong Answer,主要是dvdf 不对

第5次:终于对了

三、改进

虽然自己实现了该问题,但边界的考虑不周,总共错误了4次。

目前存在的问题,就是性能不够好。

看看其他人的实现,再写3次。

争取做到bug free,性能足够好。

下面是我的完整代码实现,需要的拿去:

#include<iostream>
using namespace std;

class Solution{
    public:
    int lengthOfLongestSubstring(string s){
        int curr = 0,len = s.length();
        int start,end,maxLength=0;
        if(len == 1){
            maxLength = 1;
            return maxLength;
        }
        while(curr<len){
            start = curr;
            end = start + 1;
            if(end>=len){
                break;
            }else{
                string sub = s.substr(start,end-start);
                while(end<len && sub.find(s.at(end)) == -1 ){
                    if(end<=len){
                        end++;
                        sub = s.substr(start,end-start);
                    }else{
                        break;
                    }
                }
                if(maxLength<(end-start)){
                    maxLength = end - start;
                }
            }
            curr++;
        }

        return maxLength;
    }
};

int main(){
    Solution s;
    cout<<s.lengthOfLongestSubstring("abcabcbb")<<endl;
    cout<<s.lengthOfLongestSubstring("bbbbb")<<endl;
    cout<<s.lengthOfLongestSubstring("pwwkew")<<endl;
    cout<<s.lengthOfLongestSubstring(" ")<<endl;
    cout<<s.lengthOfLongestSubstring("   ")<<endl;
    cout<<s.lengthOfLongestSubstring("")<<endl;
    cout<<s.lengthOfLongestSubstring("au")<<endl;
    cout<<s.lengthOfLongestSubstring("dvdf")<<endl;
    return 0;
}

网上找一个性能稍微好点也容易理解的:

#include<iostream>
#include<vector>
using namespace std;

class Solution{
    public:   //pwwkew
    int lengthOfLongestSubstring(string s){
        int res = 0;
        vector<int> m(128,0);
        for(int i=0,j=0;j<s.size();j++){
            if(m[s[j]]++ == 0){
                res = max(res,j-i+1);
            }else{
                while(i<j && m[s[j]]>1){
                    m[s[i]]--;
                    i++;
                }
            }
        }
        return res;
    }
};

int main(){
    Solution s;
    cout<<s.lengthOfLongestSubstring("abcabcbb")<<endl;
    cout<<s.lengthOfLongestSubstring("bbbbb")<<endl;
    cout<<s.lengthOfLongestSubstring("pwwkew")<<endl;
    cout<<s.lengthOfLongestSubstring(" ")<<endl;
    cout<<s.lengthOfLongestSubstring("   ")<<endl;
    cout<<s.lengthOfLongestSubstring("")<<endl;
    cout<<s.lengthOfLongestSubstring("au")<<endl;
    cout<<s.lengthOfLongestSubstring("dvdf")<<endl;
    return 0;
}

原文地址:https://www.cnblogs.com/siweihz/p/12231169.html

时间: 2024-10-10 10:17:26

刷题3. Longest Substring Without Repeating Characters的相关文章

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 s

【LeetCode刷题系列 - 003题】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 Exp

【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 l

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

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

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. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "b

leetcode_3题——Longest Substring Without Repeating Characters(set,哈希表,两个指针)

Longest Substring Without Repeating Characters Total Accepted: 62719 Total Submissions: 298285My Submissions Question Solution Given a string, find the length of the longest substring without repeating characters. For example, the longest substring w