lundi 7 septembre 2020

Stuck in a if else statement and return

I am doing a leetcode question 1022. Sum of Root To Leaf Binary Numbers. But I am kind of stuck in a very simple if else statement and return condition.

Can anyone help me to distinguish the differences of this 2 version? Thank you so much!

Version 1

 public int method() {
        return method2();
    }
    
    private int method2(){
       
        if(condition A){
            return something1;
        }
        
        return something2;
    }

Version 2(correct one)

The only difference is that the last line of the return statement is in else condition.

 public int method() {
        return method2();
    }
    
    private int method2(){
       
        if(condition A){
            return something1;
        } else {
            return something2;
        }
    }

From my understanding, version 1 and version 2 is exactly the same. However, it turns out that version is incorrect and version 2 is correct. I am kind of lost in this "dead lock".....

Can anyone help me with that? Thank you so much!

Aucun commentaire:

Enregistrer un commentaire