Sudoku algorithm in C#

前端 未结 9 1151
南方客
南方客 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:14

    This is about 50-250 times faster than a LINQ solution (depending on how early the duplicate is found):

    public static bool IsValid(int[] values) {
        int flag = 0;
        foreach (int value in values) {
            if (value != 0) {
                int bit = 1 << value;
                if ((flag & bit) != 0) return false;
                flag |= bit;
            }
        }
        return true;
    }
    

提交回复
热议问题