• 字符串问题之 找到字符串的最长无重复子串长度


    给定一个字符串str, 返回str的最长无重复字符子串长度

    例如 str="abcd' 返回4

          str=“aabcb” 最长“abc” 返回3

    解决本题的思路非常非常有趣,这种思路必须要学会:

    本题目可以做到  时间复杂度O(N)  str长度N     空间复杂度O(M)  M是字符编码

     根据字符编码 大小 申请 map  key表示字符  value表示最近出现的位置

                                     int pre 遍历到的字符str[i] 以为str[i]结尾的最长无重复字符子串开始位置的前一个位置 初始时候 pre=-1;

                                    int len 记录以每一个字符结尾的情况下 最长的长度 初始len=0

    package TT;
    
    
    
    public class Test6 {
      
        public static int maxUnique(String str){
            if(str==null || str.equals("")){
                return 0;
            }
            
            char[] chas = str.toCharArray();
            int[] map = new int[256];
            for(int i =0; i<256; i++){
                map[i]=-1;
            }
            
            int len =0;
            int pre = -1;
            int cur=0;
            for(int i =0; i!=chas.length; i++){
                
                pre = Math.max(pre, map[chas[i]]);
                cur = i-pre;
                len = Math.max(len, cur);
                map[chas[i]]=i;   
            }
            return len;
        }
        public static void main(String[] args){
            
            String aa = "abcd";
            int x = maxUnique(aa);
            System.out.println(x);
            
        }
    }

    最终结果:

     也可以这么玩儿:

    public class Test7 {
        public static int getLangest(String str) {
            HashMap<Character, Integer> hashMap = new HashMap<>();
            int lastPosition = str.length() - 1;
            int length = 0;
            int repeatPosition = 0;
            for (int i = 0; i < str.length(); i++) {
                Character now = str.charAt(i);
                if (!hashMap.containsKey(now)) {
                    hashMap.put(now, i);
                    length = (i - repeatPosition) > length ? (i - repeatPosition) : length;
                } else {
                    repeatPosition = i;
                    if (i == lastPosition) {
                    length = (i - repeatPosition) > length ? (i - repeatPosition) : length;
                    }
                    length = (i - hashMap.get(now)) > length ? (i - hashMap.get(now)) : length;
                }
    
            }
            return length+1;
    
        }
    
        public static void main(String[] args) {
            String str = "aabcbrmnv";
            System.out.println(getLangest(str));
        }
    }

     这么玩儿:

    public class Test8 {
    
        public static int getLongest(String str) {
    
            char[] strArr = str.toCharArray();
            
            int[] arr = new int[256];  //申请数组并初始化为0
            for(int i=0; i<arr.length; i++) {
                arr[i]=-1;
            }
            
            int resLength = 0;  //返回的长度
            int pre = -1;  //数组左边的以为哈哈哈哈
            for (int i = 0; i < strArr.length; i++) {
                
                char now = strArr[i];
                pre = arr[now] > pre ? arr[now] : pre;
                resLength = (i - pre) > resLength ? (i-pre) :  resLength;
                arr[now]=i;
                          
        
            }
            
            return resLength;
        }
    
        public static void main(String[] args) {
            String str = "aabcbrmnv";
            System.out.println(getLongest(str));
        
        }
    }
  • 相关阅读:
    2019-11-4:渗透测试,bypass学习,笔记
    2019-11-3:渗透测试,基础学习,bypass类型笔记
    Linux常用命令集合
    HBase(0.96以上版本)过滤器Filter详解及实例代码
    Hadoop安全模式
    Linux学习笔记--一些错误的记录
    GUG记录
    为什么 1000 == 1000会返回false,100 == 100会返回true
    关于解决mysql数据库乱码的问题
    《MVC实现用户权限》
  • 原文地址:https://www.cnblogs.com/toov5/p/7410761.html
Copyright © 2020-2023  润新知