首页 > 其他分享 >LeetCode03 无重复字符的最长子串

LeetCode03 无重复字符的最长子串

时间:2023-02-14 16:12:03浏览次数:41  
标签:子串 字符 set int max ++ right LeetCode03 left

暴力解法,O(n²)

    public int lengthOfLongestSubstring(String s) {
        ArrayList<Integer> lenList = new ArrayList<>();
        for (int i = 0; i < s.length(); i++) {
            Set<Character> set = new HashSet<>();
            int count = 0;
            for (int j = i; j < s.length(); j++) {
                char c = s.charAt(j);
                if (!set.contains(c)) {
                    set.add(c);
                    count++;
                } else {
                    lenList.add(count);
                    break;
                }
                lenList.add(count);
            }
        }
        return MaxInList(lenList);
    }

    private static int MaxInList(ArrayList<Integer> list) {
        if (list.isEmpty()) {
            return 0;
        }
        int max = list.get(0);
        for (Integer num : list) {
            if (num > max) {
                max = num;
            }
        }
        return max;
    }

滑动窗口,O(n)

public int lengthOfLongestSubstring(String s) {
        HashSet<Character> set = new HashSet<>();
        int max = 0;
        int n = s.length();
        int right = -1;
        for (int left = 0; left < n; left++) {
            if (left != 0) {
                set.remove(s.charAt(left - 1));
            }

            while ((right + 1 < n) && !set.contains(s.charAt(right + 1))) {
                set.add(s.charAt(right + 1));
                right++;
            }
            max = Math.max(right - left + 1, max);
        }

        return max;
    }

标签:子串,字符,set,int,max,++,right,LeetCode03,left
From: https://www.cnblogs.com/jrjewljs/p/16735064.html

相关文章

  • Jackson无法将LocalDateTime序列化成字符串的解决办法
     1.情景展示在web开发过程中,如果使用的是SpringBoot框架的话,我们通常使用在前后端数据交互的时候,我们通常会涉及到日期类型的转换。当我们需要将日期类型转换成字......
  • 3621、亲密字符串
    给你两个字符串s和goal,只要我们可以通过交换s中的两个字母得到与goal相等的结果,就返回true;否则返回false。交换字母的定义是:取两个下标i和j(下标从0开始)且......
  • php 字符串匹配相似度
    <?phpclassLCS{var$str1;var$str2;var$c=array();/*返回串一和串二的最长公共子序列*/functiongetLCS($str1,$str2,$len1=......
  • C/C++读入含有空格的字符串
    好久之前遇到gets()不准用的情况,所以稍稍参考了一下网上的方法,整理一下。 charst[maxn];strings;1、gets(st);2、scanf("%[^\n]",st);3、getline(cin,s);......
  • 基本类型和字符串的转换
    publicclassDemo01{publicstaticvoidmain(String[]args){//基本类型和字符串之间的转换//1.基本类型转换为字符串intnum1=15;......
  • awk多字符串分割用法示例
    多字符串分割用单引号,不要用“[]”,但多个直接仍然用竖号分隔,需要转义的也仍然用斜杠“\”,但注意需要两个斜杠“\”,因为斜杠本身也需要转义。示例:两个多字符串分割符,分别为......
  • java删除字符串最后一位
    Strings="1,2,3,4,5,6,7,8,";//目标:删除最后一个","s=s.substring(0,s.length()-1);System.out.println(s); ......
  • Python列表转换为逗号分隔的字符串(二)
    我们可以使用一个列表以一个通用名称存储不同的元素。字符串是字符的集合。在本教程中,我们将列表转换为逗号分隔的字符串。在Python中使用 join() 函数将列表转换为......
  • MySQL 替换和截取指定位置字符串
    1.情景展示返回服务器的身份证号需要进行加密:只保留前4位和后3位,中间使用*代替,如何实现? 2.场景分析需要用到的函数有:IFNULL(),IF(),LENGTH(),REPLACE(),SUBSTR()......
  • linux shell 字符串处理过滤方法
    1.grep文本过滤命令grep中的正则表达式^westos#以westos开头westos$#以westos结尾'w....s''w.....''.....s'grep-E=egrep应用:cp/etc/passwd/mntc......