Simple C# Binary Generation Solution


  • 0
    S
    public class Solution {
        public IList<IList<int>> Subsets(int[] nums) {
            
            var answers = new List<IList<int>>();
            var index = 0;
            
            while(true)
            {
                var binary = Convert.ToString(index++, 2);
                binary = binary.PadLeft(nums.Count());
                
                if(binary.Length > nums.Count())
                {
                    break;
                }
                
                var answer = new List<int>();
                for(var i = 0; i < nums.Count(); i++)
                {
                    if(binary[i] == '1')
                    {
                        answer.Add(nums[i]);
                    }
                }
                answers.Add(answer);
            }
            
            return answers;
        }
    }
    

Log in to reply
 

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