Why my java solution with hashtable time limit exception? Thank you very much!


  • 0
    S

    time limit exception

    thank you very much!

    public  int lengthOfLongestSubstring(String s) {
            if(s.length()<2){
                return s.length();
            }
             
             int max=0;
             Map<String,Integer> map = new HashMap<String,Integer>();
             for(int k=0;k<s.length();){
            	 String ch=s.substring(k,k+1);
                 Object o = map.get(ch);
                 if(o==null){
                     map.put(ch,k);
                     k++;
                     continue;
                 }
                 if(max<map.size()){
                     max=map.size();
                 }
                 map.clear();
                 k=(int)o+1;
             }
            
          //  System.out.println("length : "+max);
             return max;
        }

  • 0
    S

    i have find the answer~ logic problem~ thank you~


Log in to reply
 

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