[leetcode] longest substring without repeating characters

2人阅读 评论(0) 收藏 举报
分类:

题目地址:

https://leetcode.com/problems/longest-substring-without-repeating-characters/description/

题目的意思很简单,也很好理解,就是找到字符串中最长且不重复的子串的长度。

方法一:
思路:遍历字符串,从第一个开始,然后往后查找,如果找到重复的,记录下当前不重复子串的长度,退出当前循环,情况共计数器,然后从第二个开始进行遍历,重复这个过程,直到整个字符串结束,完成最长子串的查找。

int lengthOfLongestSubstring(string s)
{
    std:set<char> sub;
    int count = 0, max = 0;
    for(int i = 0; i < s.size(); i ++) {
        for(int j = i; j < s.size(); j++) {
            if(sub.find(s[j]) == sub.end())
            {
                count ++;
                sub.insert(s[j]);
                if(count > max) {
                    max = count;
                }
            }
            else {
                break;
            }
        }
        count = 0;
        sub.clear();
    }
    return max;
}
查看评论

ASP.NET之‘字符串’

-
  • 1970年01月01日 08:00

leetcode 3 Longest Substring Without Repeating Characters最长无重复子串

Given a string, find the length of the longest substring without repeating characters. For example, ...
  • wangyaninglm
  • wangyaninglm
  • 2016-04-05 22:27:27
  • 3780

[LeetCode] 003. Longest Substring Without Repeating Characters (Medium) (C++/Java/Python)

[LeetCode] 003. Longest Substring Without Repeating Characters (Medium) (C++/Java/Python)
  • hcbbt
  • hcbbt
  • 2015-02-27 12:55:12
  • 6039

LeetCode 3.Longest Substring Without Repeating Characters c语言版

问题描述: Given a string, find the length of the longest substring without repeating characters. Exa...
  • banana1006034246
  • banana1006034246
  • 2017-07-15 17:35:47
  • 709

LeetCode(3) Longest Substring Without Repeating Characters

LeetCode的第3题,给定一个字符串,找到其中的一个最长的字串,使得这个子串不包含重复的字符。 // // Solution.h // LeetCodeOJ_003_LongestUnique...
  • feliciafay
  • feliciafay
  • 2013-11-23 04:25:39
  • 17070

leetcode:3. Longest Substring Without Repeating Characters (java)

题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目: Given a str...
  • j754379117
  • j754379117
  • 2016-06-20 20:36:28
  • 1355

LeetCode--longest-substring-without-repeating-characters

题目描述 Given a string, find the length of the longest substring without repeating charact...
  • wl_ss
  • wl_ss
  • 2018-01-19 22:53:48
  • 85

Longest Substring Without Repeating Characters -- LeetCode

原题链接: http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/  这道题用的方法是在Leet...
  • linhuanmars
  • linhuanmars
  • 2014-02-26 01:53:37
  • 19167

leetcode 3 : Longest Substring Without Repeating Characters 最长无重复子串 (C# 语言版)

leetcode第三题,自己的算法,不知道复杂度怎样,希望能与网友交流。
  • hellowangxyue
  • hellowangxyue
  • 2016-04-05 09:47:32
  • 636

LeetCode 第三题,Longest Substring Without Repeating Characters

题目: Given a string, find the length of the longest substring without repeating characters. For examp...
  • hu1020935219
  • hu1020935219
  • 2014-08-03 20:38:44
  • 8099
    个人资料
    持之以恒
    等级:
    访问量: 1414
    积分: 239
    排名: 30万+