C# Study code from ranking No. 1 in weekly contest 128
March 21, 2019\nIt is one of weekly contest 128 algorithms. What I like to do is to learn ranking No. 1\'s code, and then I add some comment to explain the idea
March 21, 2019\nIt is one of weekly contest 128 algorithms. What I like to do is to learn ranking No. 1\'s code, and then I add some comment to explain the idea to make it easy to follow. The player finished the algorithm using 7 minutes 5 seconds. \n\n![image](https://assets.leetcode.com/users/jianminchen/image_1553206027.png)\n\n\nHere are a few tips:\n1. using one binary number to store all unique digits in the number; mapping is described in the comment\n2. using & operator to avoid any duplicated digit\n3. using or to include current digit into the binary number\n\n**More detail**\nEvery number from 0 to 9 can be represented using one bit in a binary number. So all digits in any  integer number can be recorded using one binary number including 10 bits at most. \n\n0 is expressed in 0, in binary format, 1 << 0\n1 is expressed in 10, in binary format, 1 << 1\n2 is expressed in 100\n3 is expressed in 1000\n4 is expressed in 10000\n5 is expressed in 100000\n6 is expressed in 1000000\n7 is expressed in 10000000\n8 is expressed in 100000000\n9 is expressed in 1000000000, in binary format, 1 << 9\n\n**How to determine current digit is not duplicated**\n\nUsing one binary number to express all unique digits in the number, and then using **and &** operator to check uniqueness, and **or | operator** to include current digits. \n\nLet us work on one example in the following. \nChoose a number with unique digits, for example, 123 or 321 or 213. All digits in the number can be expressed using one binary number 1110, so next digit should avoid duplication of those three digits. \n\nIf current digit is 4, the exiting digits include {1, 2, 3}, then 1110 & (1 << 4) = 1110 & 10000 = 0, \nsince there is no duplication of digits. However, any digit from {1, 2, 3}, its binary format number & 1110 > 0.\n\nIt is the excellent practice to warm up bit manipulation and learn how to write a solution in less than 10 minutes. Othewise, it is better to use standard depth first search, and use an array instead of a binary number to mark the digit used. I also posted the solution on that: https://leetcode.com/problems/numbers-with-repeated-digits/discuss/259737/C-standard-depth-first-search-with-back-tracking\n\nHere is my C# code based on code study. \n\t\t\t\t\n```\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Text;\nusing System.Threading.Tasks;\n\nnamespace _1015_number_with_repeated_digits_1\n{\n    class Program\n    {\n        static void Main(string[] args)\n        {\n            var result = NumDupDigitsAtMostN(20);\n        }\n\n        public static int n;\n        public static int uniqueDigitis;\n\n        /// <summary>\n        /// study code on weekly contest 128 rank No. 1 waakaaka\n        /// https://leetcode.com/contest/weekly-contest-128/ranking\n        /// </summary>\n        /// <param name="N"></param>\n        /// <returns></returns>\n        public static int NumDupDigitsAtMostN(int N)\n        {\n            uniqueDigitis = 0;\n            n = N;\n            runDepthFirstSearchLeftToRight(0,0);\n            return N + 1 - uniqueDigitis; \n        }\n\n        /// <summary>\n        /// code review March 21, 2019\n        /// Here are a few tips:\n        /// 1. using one binary number to store all unique digits in the number; mapping is described in the comment\n        /// 2. using & operator to avoid any duplicated digit\n        /// 3. using or to include current digit into the binary number\n        /// </summary>\n        /// <param name="value"></param>\n        /// <param name="binaryShift"></param>\n        private static void runDepthFirstSearchLeftToRight(long value, int binaryShift)\n        {\n            if (value <= n)\n            {\n                uniqueDigitis++;\n            }\n\n            if (value * 10 > n)\n            {\n                return;\n            }\n\n            for (int digit = 0; digit <= 9; digit++)\n            {\n                // no 0 for first digit\n                if (binaryShift == 0 && digit == 0)\n                {\n                    continue;\n                }\n\n                // every number from 0 to 9 is represented using binary number\n                // 0 is expressed in 0\n                // 1 is expressed in 10\n                // 2 is expressed in 100\n                // 3 is expressed in 1000\n                // 4 is expressed in 10000\n                // 5 is expressed in 100000\n                // 6 is expressed in 1000000\n                // 7 is expressed in 10000000\n                // 8 is expressed in 100000000\n                // 9 is expressed in 1000000000\n                var currentDigitExpression = 1 << digit; \n               \n                // number with unique digits, for example, 123 or 321 or 213. \n                // all digits in the number can be expressed using one binary number 1110\n                // so next digit should avoid duplication of those three digits\n                // if current digit is 4, the exiting digits include {1, 2, 3}, then 1110 & (1 << 4) = 1110 & 10000 = 0, \n                // since there is no duplication of digits                \n                // any digit from {1, 2, 3} & 1110 > 0 \n                if ((binaryShift & currentDigitExpression) > 0)\n                {\n                    continue; \n                }\n\n                // using | to include current digit\n                var nextBinary = binaryShift | currentDigitExpression;\n                runDepthFirstSearchLeftToRight(value * 10 + digit, nextBinary); \n            }\n        }\n    }\n}\n```
[]