code in java cost 93ms in eclipse. but fails because of time exceed??


  • 0
    E

    it just runs 93ms in eclipse.but why say it exceed time??

    public int lengthOfLongestSubstring(String s) {
    	        Map<Character,Integer> map=new HashMap<Character,Integer>();
    	        int i=0,maxCount=0;
    	        char[] array=s.toCharArray();
    	        for(char iter:array){
    	            if(map.containsKey(iter)){
    	                if(map.size()>maxCount)maxCount=map.size();
    	                int cleanInd=map.get(iter);
    	                while(cleanInd>=0&&map.containsKey(s.charAt(cleanInd))){
    	                if(map.get(s.charAt(cleanInd))==cleanInd)//Donnot forget this!!!!!!!!
    	                    map.remove(s.charAt(cleanInd--));
    	                else
    	                    cleanInd--;
    	                }
    	            }
    	            map.put(iter,i++);
    	        }
    	        if(map.size()>maxCount)maxCount=map.size();     //Donnot forget this!!!!!!!!
    	        return maxCount;
    	    }

Log in to reply
 

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