zoukankan      html  css  js  c++  java
  • leetcode(30)串联所有单词的子串

    串联所有单词的子串

    解题思路:滑动窗口以及Hash

    class Solution {
        public List<Integer> findSubstring(String s, String[] words) {
            List<Integer> result = new ArrayList<>();
            if(s.length()==0||words.length==0||words[0].length()==0){
                return result;
            }
            int slen = s.length();
            int wordLen = words.length;
            int itemLen = words[0].length();
            Integer t = null;
            String temps = null;
            int n = 0;
            Map<String,Integer> map = new HashMap<>();
            Map<String,Integer> map2 = new HashMap<>();
            String[] strs = new String[slen-itemLen+1];
            for(int i=0;i<wordLen;i++){
                t = map.get(words[i]);
                if(t==null){
                   map.put(words[i],1); 
                }else{
                   map.put(words[i],t+1);
                }
            }
            for(int i=0;i<slen-itemLen+1;i++){
                temps = s.substring(i,i+itemLen);
                if(map.containsKey(temps)){
                    strs[i] = temps;
                }
            }
            for(int i=0;i<slen-wordLen*itemLen+1;i++){
                map2.clear();
                n=0;
                temps = strs[i+n*itemLen];
                while(temps!=null){ 
                    t = map2.get(temps);
                    if(t==null){
                        map2.put(temps,1);
                    }else{
                        if(t+1>map.get(temps)){
                            break;
                        }
                        map2.put(temps,t+1);
                    }
                    n++;
                    if(n==wordLen){
                        result.add(i);
                        break;
                    }
                    temps = strs[i+n*itemLen];
                }
            }
            return result;
        }
    }

    第二种方法:

    解题思路:在第一种基础上,不清空Hash表,而是逐渐调整Hash表,这样子会节省大量的时间。

    class Solution {
        public List<Integer> findSubstring(String s, String[] words) {
            List<Integer> result = new ArrayList<>();
            if(s.length()==0||words.length==0||words[0].length()==0){
                return result;
            }
            int slen = s.length();
            int wordLen = words.length;
            int itemLen = words[0].length();
            Integer t = null;
            String temps = null;
            int n = 0;
            int start = 0;
            int index = 0;
            Map<String,Integer> map = new HashMap<>();
            Map<String,Integer> map2 = new HashMap<>();
            String[] strs = new String[slen-itemLen+1];
            for(int i=0;i<wordLen;i++){
                t = map.get(words[i]);
                if(t==null){
                   map.put(words[i],1); 
                }else{
                   map.put(words[i],t+1);
                }
            }
            for(int i=0;i<slen-itemLen+1;i++){
                temps = s.substring(i,i+itemLen);
                if(map.containsKey(temps)){
                    strs[i] = temps;
                }
            }
            for(int i=0;i<itemLen;i++){
                map2.clear();
                n=0;
                start = i;
                while(start+n*itemLen<slen-itemLen+1&&start<slen-wordLen*itemLen+1){
                    index = start+n*itemLen;
                    if(strs[index]==null){
                        map2.clear();
                        n=0;
                        start = index+itemLen;
                    }else{
                        t = map2.get(strs[index]);
                        if(t==null){
                            map2.put(strs[index],1);  
                        }else{
                            if(t+1>map.get(strs[index])){
                                while(!strs[start].equals(strs[index])){
                                    t = map2.get(strs[start]);
                                    map2.put(strs[start],t-1);  
                                    start +=itemLen;
                                    n--;
                                }
                                t = map2.get(strs[index]);
                                map2.put(strs[index],t-1);
                                start +=itemLen;
                                n--;
                            }
                            t = map2.get(strs[index]);
                            map2.put(strs[index],t+1);   
                        }
                        n++;
                        if(n==wordLen){
                            result.add(start);
                        }
                    }
                }
                
            }
            return result;
        }
    }
  • 相关阅读:
    Linux安全应用之防垃圾邮件服务器的构建
    Postfix邮件系统安装配置视频
    Linux常用的安全工具
    Linux系统安全加固(一)
    全球开源软件发展趋势分析
    安装配置FreeBSD9全过程体验
    P1441-砝码称重
    POJ-2376 Cleaning Shifts
    P1514-引水入城
    P1378-油滴扩展
  • 原文地址:https://www.cnblogs.com/erdanyang/p/11097560.html
Copyright © 2011-2022 走看看