Validating O/p with wrong input - leetcode issue


  • 0
    N

    Below is my code :
    class Solution {
    public int calPoints(String[] ops) {

        for(int k = 0 ; k < ops.length ;k++){
            System.out.println(ops[k]);
        }
        
        LinkedList<String> link = new LinkedList<String>();
        int resultSum = 0;
         for(int i = 0; i < ops.length; i++){
                if(checkInt(ops[i])){
                    //System.out.println("Output : "+ ops[i] + " Value : " + Integer.parseInt(ops[i]));
                    resultSum = resultSum + Integer.parseInt(ops[i]);
                    link.addLast(ops[i]);
                }else{
                    if(ops[i].equals("C")){
                        // remove last valid character and make it invalid    
                        String val = link.removeLast();
                        resultSum = resultSum - Integer.parseInt(val);
                    }else if(ops[i].equals("D")){
                        //add double amout to last valid points
                        String val = link.getLast();
                        resultSum = resultSum + (2 * Integer.parseInt(val));
                        link.addLast(new Integer(2 * Integer.parseInt(val)).toString());
                    }else if(ops[i].equals("+")){
                        //add last two valid sum into the result
                        String val = link.removeLast();
                        String val2 = link.removeLast();
                        Integer bb = Integer.parseInt(val)+Integer.parseInt(val2);
                        resultSum = resultSum + bb;
                        link.addLast(val);
                        link.addLast(new Integer(bb).toString());
                    }
                }
             
         }
        return resultSum;
        
    }
    public boolean checkInt(String val){
        try{
            Integer.parseInt(val);
            return true;
        }catch(Exception e){
            return false;
        }
    }
    

    }

    test case showing as input : ["35","-32","C","39","+","-48","C","5","88","-52","+","C","D","D","100","C","+","-74","53","59"]

    Expected O/p : -397 (My code is give correct o/p for this test case.

    Actual input which I am receiving

    ["57","D","-3","-58","D","77","+","C","+","+","38","78","-6","24","-46","+","31","20","D","-81"]

    Can somebody tell me what to do in this situation? I am totally out of contest ....


  • 0
    N

    I have solve this problem. Thanks for looking into it


  • 1
    9

    How do you solve it ?
    I have same issue in Java.


Log in to reply
 

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