My C# Solution using recursion


  • 0
    public class Solution {
        public IList<string> FindStrobogrammatic(int n) {
            var ans = new List<string>();
            if(n==0) return ans;
            char[] str = new char[n];
            GenStrobo(str,ans,0,n-1);
            return ans;
        }
        
        void GenStrobo(char[] str, IList<string> ans, int l, int r) {
            int[,] nums = {{1,1},{6,9},{8,8},{9,6},{0,0}};
            if(r < l) {
                ans.Add(new string(str));
                return;
            }
            for(int i=0; i < nums.GetLength(0); i++) {
                if(l==r && (nums[i,0]==6 || nums[i,0]==9)) continue; //skip 6 and 9 for middle char
                if(l==0 && nums[i,0]==0 && str.Length > 1) continue; //skip 0 for the first digit
                str[l] = (char)(nums[i,0] + '0');
                str[r] = (char)(nums[i,1] + '0');
                GenStrobo(str,ans,l+1,r-1);
            }
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.