<Java>Time Limit Exceeded,why


  • 1
    T
     public class Solution {
    
      public int numDecodings(String s) {
      return numDecodings1( s ,0);
     }
      public int numDecodings1(String s ,int fromIndex) {
        int sum = 0;
        if (fromIndex + 1 < s.length()) {
             char firstDigit= s.charAt(fromIndex) ;
             char secondDigit= s.charAt(fromIndex + 1);
            if (firstDigit=='0') {
                return 0;
            } else if (firstDigit=='1'||(firstDigit=='2'&&secondDigit<'7')) {
                if (secondDigit=='0') {
                    if (fromIndex + 2 < s.length()) {
                        sum += numDecodings1(s, fromIndex + 2);
                    }
                    if (fromIndex + 2 == s.length()) {
                        sum += 1;
                    }
                } else {
                    if (fromIndex + 2 < s.length()) {
                        sum += numDecodings1(s, fromIndex + 2);
                    } else if (fromIndex + 2 == s.length()) {
                        sum += 1;
                    }
                    sum += numDecodings1(s, fromIndex + 1);
                }
            } else {
                if (secondDigit=='0') {
                     return 0;
                } else {
                    sum += numDecodings1(s, fromIndex + 1);
                }
            }
        } else if (fromIndex < s.length()) {
            if (s.charAt(fromIndex) != '0') {
                sum += 1;
            }
        }
    
        return sum;
    }
    

    }


Log in to reply
 

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