Why my code will exceed memory limit?


  • 0
    W
    public class Solution {
        public List<String> findRepeatedDnaSequences(String s) {
            HashMap<Integer, Integer> map=new HashMap<Integer, Integer>();
            List<String>r=new LinkedList<String>();
            List<Character>chs=new LinkedList<Character>();
            final int len=s.length();
            if(len<=10){
                return r;
            }
            int count=0;
            for(int i=0;i<len;++i){
                if(count<10){
                    count++;
                    chs.add(s.charAt(i));
                    if(count==10){
                        map.put(hashCode(chs), 0);
                    }
                } else {
                    chs.add(s.charAt(i));
                    chs.remove(0);
                    int code=hashCode(chs);
                    if(!map.containsKey(code)){
                        map.put(code, 0);
                    } else {
                        if(map.get(code)==0){
                            r.add(getString(chs));
                            map.put(code, 1);
                        }
                    }
                }
            }
            return r;
        }
        
        public String getString(List<Character> str){
            sb.setLength(0);
            for(Character c:str){
                sb.append(c);
            }
            return sb.toString();
        }
        StringBuilder sb=new StringBuilder();
        public int hashCode(List<Character> str){
            sb.setLength(0);
            for(Character c:str){
                sb.append((map(c)));
            }
            return Integer.valueOf(sb.toString(),2);
           // return 0;
        }
        
        public String map(char c){
            if(c=='A'){
                return "00";
            }
            if(c=='C'){
                return "01";
            }
            if(c=='G'){
                return "10";
            }
            if(c=='T'){
                return "11";
            }
            return "";
        }
    }
    

    I don't know why it will exceed memory limit. Need help.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.