RunTime Error with Input ")("


  • 0
    S

    The code below when taken on C# works fine in VS, but here it is giving RuntIme Error. Can somebody point the issue

    { public int longestValidParentheses(String s) {

        if(s== null || s.length() < 2) return 0;
        
        Stack<Integer> st = new Stack<Integer>();
        
        int left = 0;
        
        Boolean[] record = new Boolean[s.length()];
        for(int i = 0; i< s.length(); i++)
        {
            if(s.charAt(i) == '(')
            {
                left++;
                st.push(i);
            }
            else if(st.isEmpty() == false)
            {
                int idx = st.pop();
                record[idx] = true;
                record[i] = true;
                
                left--;
            }
        }
        
        int result = 0;
        int count = 0;
        
        for (int i = 0; i < record.length; i++)
        {
            if(record[i] == true)
            {
                count++;
            }
            else
            {
                if(count > result)
                {
                    result = count;
                }
                
                count = 0;
            }
        }
        
        return result;
    }}

  • 0

    Please format your code by selecting your code and click on the {} button. Please explain your code briefly using a few sentences or adding some comments in your code so others can understand and help you.


  • 0
    L

    Define the array record by boolean[] record = new boolean[s.length()]; with a lowercase b for boolean. That will do the trick.

    Your variable left is useless. It makes your code harder to read. Please when you ask question here, first describe your algorithm in plain english fist and add comments. You might have realized that left is useless by yourself.

    In Java, you shouldn't use the class Stack, it's better to use a class that implements the Deque interface. Have a look at ArrayDeque.

    It is a bad coding habit to test equality between a boolean variable and a boolean constant. You should replace line like if (a == false) by if (!a) and lines like if (b == true) by if (b).

    Your code won't give the right answer to the problem. Try to figure out why before reading the solution below.

    for (int i = 0; i < record.length; i++)  {
        if (record[i]) {
            count++;
            result = Math.max(result,count);
        }  else {
            count = 0;
        }
    }
    

  • 0
    S

    I still did not get why Boolean array was throwing exception but boolean array did not.


  • 0
    L

    Me neither. OJ has been updated yesterday and there are some weird errors since. Anyways, Boolean[] instead of boolean[] here was bad code (objects creation, boxing, and unboxing are expensive operations) Take it as the judge telling you that you could do better ;-)


Log in to reply
 

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