真相只有一个

来源:互联网 发布:linux mysql 命令 编辑:程序博客网 时间:2024/04/29 01:16

描述:

在一个布尔数组中,有且仅有一个true的情况下,返回true,否则返回false

例如:

onlyOne() –> false
onlyOne(true, false, false) –> true
onlyOne(true, false, false, true) –> false
onlyOne(false, false, false, false) –> false

MyCode:

public class Kata{  public static bool OnlyOne(params bool[] flags)  {    return flags.Count(i => i == true) == 1 ? true : false;  }}

CodeWar:

using System.Linq;public class Kata{  public static bool OnlyOne(params bool[] flags)  {    return flags.Count(x => x) == 1;  }}
0 0
原创粉丝点击