Check if at least two out of three booleans are true

后端 未结 30 1604
自闭症患者
自闭症患者 2020-11-28 16:47

An interviewer recently asked me this question: given three boolean variables, a, b, and c, return true if at least two out of the three are true.

My solution follow

相关标签:
30条回答
  • 2020-11-28 17:25
    return (a==b) ? a : c;
    

    Explanation:

    If a==b, then both are true or both are false. If both are true, we have found our two true booleans, and can return true (by returning a). If both are false there cannot be two true booleans even if c is true, so we return false (by returning a). That's the (a==b) ? a part. What about : c ? Well if a==b is false, then exactly one of a or b must be true, so we have found the first true boolean, and the only thing left that matters is if c is also true, so we return c as the answer.

    0 讨论(0)
  • 2020-11-28 17:26

    Just for the sake of using XOR to answer a relatively straight-forward problem...

    return a ^ b ? c : a
    
    0 讨论(0)
  • 2020-11-28 17:26

    Another example of direct code:

    int  n = 0;
    if (a) n++;
    if (b) n++;
    if (c) n++;
    return (n >= 2);
    

    It's not the most succinct code, obviously.

    Addendum

    Another (slightly optimized) version of this:

    int  n = -2;
    if (a) n++;
    if (b) n++;
    if (c) n++;
    return (n >= 0);
    

    This might run slightly faster, assuming that the comparison against 0 will use faster (or perhaps less) code than the comparison against 2.

    0 讨论(0)
  • 2020-11-28 17:26

    I don't think I've seen this solution yet:

    boolean atLeast(int howMany, boolean[] boolValues) {
      // check params for valid values
    
      int counter = 0;
      for (boolean b : boolValues) {
        if (b) {
          counter++;
    
          if (counter == howMany) {
            return true;
          }
        }
      }
      return false;
    }
    

    Its advantage is that once it reaches the number that you're looking for, it breaks. So if this was "at least 2 out of this 1,000,000 values are true" where the first two are actually true, then it should go faster than some of the more "normal" solutions.

    0 讨论(0)
  • 2020-11-28 17:26
    Function ReturnTrueIfTwoIsTrue(bool val1, val2, val3))
    {
         return (System.Convert.ToInt16(val1) +
                 System.Convert.ToInt16(val2) +
                 System.Convert.ToInt16(val3)) > 1;
    }
    

    Too many ways to do this...

    0 讨论(0)
  • 2020-11-28 17:28

    As an addition to @TofuBeer TofuBeer's excellent post, consider @pdox pdox's answer:

    static boolean five(final boolean a, final boolean b, final boolean c)
    {
        return a == b ? a : c;
    }
    

    Consider also its disassembled version as given by "javap -c":

    static boolean five(boolean, boolean, boolean);
      Code:
        0:    iload_0
        1:    iload_1
        2:    if_icmpne    9
        5:    iload_0
        6:    goto    10
        9:    iload_2
       10:    ireturn
    

    pdox's answer compiles to less byte code than any of the previous answers. How does its execution time compare to the others?

    one                5242 ms
    two                6318 ms
    three (moonshadow) 3806 ms
    four               7192 ms
    five  (pdox)       3650 ms
    

    At least on my computer, pdox's answer is just slightly faster than @moonshadow moonshadow's answer, making pdox's the fastest overall (on my HP/Intel laptop).

    0 讨论(0)
提交回复
热议问题