首页 > 其他分享 >3. Longest Substring Without Repeating Characters [Medium]

3. Longest Substring Without Repeating Characters [Medium]

时间:2023-01-12 01:22:17浏览次数:42  
标签:Medium String int Substring right result 指针 Without left

3. Longest Substring Without Repeating Characters

Given a string s, find the length of the longest substring without repeating characters.

Constraints:

  • 0 <= s.length <= 5 * 10^4
  • s consists of English letters, digits, symbols and spaces.

Example

Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

思路

  • 是求连续子串不是子序列
  • 左右指针来截取字符串,右指针每要添加进一个字符时,先看一下当前截取字符串中是否已存在,如果存在就左指针移一位

题解

  • 无脑快速AC
    public int lengthOfLongestSubstring(String s) {
        int left, right, result;
        left = right = result = 0;
        String temp;

        while (right < s.length()) {
	    // 获取右指针字符,也就是待插入字符
            String cur = String.valueOf(s.charAt(right));
	    // 截取当前左指针到右指针前1位的字符串
            temp = s.substring(left, right);
	    // 当前子串中包含待插入字符,移动左指针
            if (temp.contains(cur)) {
                left++;
            } else {
		// 直到符合条件,那就算出当前子串长度
                result = Math.max(result, right - left + 1);
                right++;
            }
        }
        return result;
    }
  • 优化
    public int lengthOfLongestSubstring(String s) {
        int result, left;
	// 利用Set特性来简化,不依靠截取字符串函数
        HashSet<Character> record = new HashSet<>();
        result = left = 0;

        char[] data = s.toCharArray();
	// i就可以当作右指针
        for (int i = 0; i < data.length; i++) {
	    // 如果Set中已存在当前待插入元素,返回false,那就从最左边开始移出,直到成功插入,最坏情况就是Set被清空,然后当前i成为Set中唯一元素
            while (!record.add(data[i])) {
                record.remove(data[left]);
                left++;
            }
            result = Math.max(result, i - left + 1);
        }
        return result;
    }

标签:Medium,String,int,Substring,right,result,指针,Without,left
From: https://www.cnblogs.com/tanhaoo/p/17044353.html

相关文章

  • [LeetCode] 2272. Substring With Largest Variance
    Thevarianceofastringisdefinedasthelargestdifferencebetweenthenumberofoccurrencesofany2characterspresentinthestring.Notethetwochara......
  • 11. Container With Most Water [Medium]
    217.ContainerWithMostWaterYouaregivenanintegerarrayheightoflengthn.Therearenverticallinesdrawnsuchthatthetwoendpointsoftheithline......
  • 15. 3Sum [Medium]
    3SumGivenanintegerarraynums,returnallthetriplets[nums[i],nums[j],nums[k]]suchthati!=j,i!=k,andj!=k,andnums[i]+nums[j]+nums[k]==......
  • *128. Longest Consecutive Sequence [Medium]
    128.LongestConsecutiveSequenceGivenanunsortedarrayofintegersnums,returnthelengthofthelongestconsecutiveelementssequence.Youmustwriteana......
  • 271. Encode and Decode Strings [Medium]
    271.EncodeandDecodeStrings居然要premium才能做,果断换LintCode来写这题Designanalgorithmtoencodealistofstringstoastring.Theencodedstringisthe......
  • 238. Product of Array Except Self [Medium]
    238.ProductofArrayExceptSelfGivenanintegerarraynums,returnanarrayanswersuchthatanswer[i]isequaltotheproductofalltheelementsofnumse......
  • 347. Top K Frequent Elements [Medium]
    347.TopKFrequentElementsGivenanintegerarraynumsandanintegerk,returnthekmostfrequentelements.Youmayreturntheanswerinanyorder.Constra......
  • 49. Group Anagrams [Medium]
    49.GroupAnagramsGivenanarrayofstringsstrs,grouptheanagramstogether.Youcanreturntheanswerinanyorder.AnAnagramisawordorphraseformedb......
  • “Code without tests is broken by design.”
    https://docs.djangoproject.com/en/4.1/intro/tutorial05/it’snevertoolatetogetstarted.今天主要接上一章节~从testing这篇官方文档开始看起。这一个测试讲述......
  • SqlServer的substring用法
    SUBSTRING(expression,start,length) 参数expression字符串、二进制字符串、文本、图像、列或包含列的表达式。请勿使用包含聚合函数的表达式。 start整数......