zoukankan      html  css  js  c++  java
  • LeetCode-Longest Substring Without Repeating Characters

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

    Example:

    Given "abcabcbb", the answer is "abc", which the length is 3.

    Given "bbbbb", the answer is "b", with the length of 1.

    Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

    我自己写的一个方法是爆搜,但是这个方法超时了。

    代码如下:

    package com.leetcode.study;
    
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.Set;
    
    public class Main {
    
        public static void main(String[] args) {
            System.out.println(lengthOfLongestSubstring("wllxdiklosdrdxfohgwringzefwbytmwgxtjhdxwycpbawphcnbmajmeokhoftlmsexakuyixplxmagoojdospvjbcxh"));
            
    
        }
        public static int lengthOfLongestSubstring(String s){
            Map<String,Integer> map = new HashMap<String, Integer>();
            for(int i = 0;i<s.length();i++){
                if(map.containsKey(s.charAt(i)+"")){
                    continue;
                }else{
                    map.put(s.charAt(i)+"", i);
                }
            }
            Set<String> set;
            int length = 0;
            boolean flag = false;
            for(int i = map.size();i>0;i--){
                for (int j = 0; j < s.length()-i+1; j++) {
                    String temp = s.substring(j, j+i);
                    set = new HashSet<String>();
                    for(int k=0;k<temp.length();k++){
                        set.add(temp.charAt(k)+"");
                    }
                    if(set.size()==temp.length()){
                        length=set.size();
                        flag=true;
                        break;
                    }
                    
                }
                if(flag){
                    break;
                }
            }
            
            return length;
            
        }
        
        
    
    }

    代码运行超时。

    还是看看别人的代码吧。

    滑动窗口算法:

    package com.leetcode.study;
    
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.Set;
    
    public class Main {
    
        public static void main(String[] args) {
            System.out.println(lengthOfLongestSubstring("abcabcbb"));
            
    
        }
        public static int lengthOfLongestSubstring(String s){
            int n = s.length();
            Set<Character> set = new HashSet<Character>();
            int ans=0,i=0,j=0;
            while(i<n&&j<n){
                if(!set.contains(s.charAt(j))){
                    set.add(s.charAt(j));
                    j++;
                    ans=Math.max(ans, j-i);
                }else{
                    set.remove(s.charAt(i));
                    i++;
                }
            }
            return ans;
            
        }
        
        
    
    }

    这个方法没太弄明白。

  • 相关阅读:
    Java核心技术 卷一 笔记四 库类的直接使用
    Java核心技术 卷一 笔记三 大数值及数组
    Java核心技术 卷一 笔记2 字符串的复制
    Java核心技术 卷一 笔记1
    修改css 样式后, hover事件 不生效
    修改 element ui input 输入框 样式不生效问题
    css3 计算属性
    Vue3 改动系列
    浏览器实现,向下滑动 鼠标滚轮,页面横向移动
    linux ceont0s7 vue 打包压缩图片 一直报错
  • 原文地址:https://www.cnblogs.com/LoganChen/p/8794522.html
Copyright © 2011-2022 走看看