Sudoku algorithm in C#

前端 未结 9 966
南方客
南方客 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 16:07

    I usually frown on solutions that involve captured variables, but I had an urge to write this:

    bool hasRepeating = false;
    int previous = 0;
    
    int firstDuplicateValue = a
      .Where(i => i != 0)
      .OrderBy(i => i)
      .FirstOrDefault(i => 
      {
        hasRepeating = (i == previous);
        previous = i;
        return hasRepeating;
      });
    
    if (hasRepeating)
    {
      Console.WriteLine(firstDuplicateValue);
    }
    

提交回复
热议问题