Sudoku algorithm in C#

前端 未结 9 1204
南方客
南方客 2021-02-03 15:24

I need one liner (or close to it) that verifies that given array of 9 elements doesn\'t contain repeating numbers 1,2,3,...,9. Repeating zeroes do not count (they represent empt

9条回答
  •  眼角桃花
    2021-02-03 15:59

    How about:

    var itIsOk = a.Where(x => x > 0).Distinct().Count() == a.Where(x => x > 0).Count();
    

    Reasoning: First create an enumeration without 0s. Out of the remaining numbers, if its distinct list is the same length as the actual list, then there are no repeats.

    or: If the list of unique numbers is smaller than the actual list, then you must have a repeated number.

    This is the one-liner version. The a.Where(x=>x>0) list could be factored out.

    var nonZeroList = a.Where(x => x > 0).ToList();
    var itIsOk = nonZeroList.Distinct().Count() == nonZeroList.Count();
    

提交回复
热议问题