DavidDeMar
BAN USERSolution in C#:
using System;
using System.Collections.Generic;
public class Program
{
public static List<List<int>> sets = new List<List<int>>();
public static void FindSubsets(int[] superSet, int K)
{
FindSubsets(superSet, K, 0, new List<int>());
}
private static void FindSubsets(int[] superSet, int K, int index, List<int> set)
{
if (set.Count == K)
{
sets.Add(new List<int>(set));
return;
}
if (index == superSet.Length) return;
int num = superSet[index];
set.Add(num);
FindSubsets(superSet, K, index + 1, set);
set.Remove(num);
FindSubsets(superSet, K, index + 1, set);
}
public static void Main()
{
var nums = new int[] { 1, 2, 3, 4, 5, 6 };
FindSubsets(nums, 3);
foreach(var set in sets)
{
foreach (var num in set) Console.Write(num);
Console.WriteLine();
}
Console.ReadLine();
}
}
private static bool IsPalindrome(string input)
{
int right = input.Length - 1;
for(int left = 0; left < input.Length / 2; left++)
{
while(!Char.IsLetter(input[left]))
{
left++;
}
while(!Char.IsLetter(input[right]))
{
right--;
}
if(Char.ToLower(input[left]) != Char.ToLower(input[right--]))
{
return false;
}
}
return true;
}
public class ClothesList
{
private SortedDictionary<string, int> clothesCounts = new SortedDictionary<string, int>();
public void GetCounts(string input)
{
var clothes = input.Split(new string[] {Environment.NewLine}, StringSplitOptions.None);
foreach(string item in clothes)
{
if (!clothesCounts.ContainsKey(item))
{
clothesCounts.Add(item, 0);
}
clothesCounts[item]++;
}
}
public void PrintCounts()
{
foreach(var item in clothesCounts)
{
if (item.Key.Contains("sock"))
{
if (item.Value > 1)
{
Console.WriteLine("{0}|{1}", item.Value / 2, item.Key);
}
if (item.Value % 2 != 0)
{
Console.WriteLine("{0}|{1}", 0, item.Key);
}
}
else
{
Console.WriteLine("{0}|{1}", item.Value, item.Key);
}
}
}
}
This will create a doubly linked list in place. The node's left property points to the previous node while the right property points to the next node. This approach is exactly like an in-order traversal except instead of printing out each node's value we replace that with logic that creates a double link between two nodes.
1) If the current node is null, return.
2) Recursively traverse the left nodes.
3) If the head of the linked list is null then make the current node the head (this will only happen once).
4) If the previous node is not null, make the current node's left property point to the previously traversed node. Then make the previously traversed node's right property point to the current node. Now the previous node and the current node are doubly linked.
5) Make the current node equal the previous node.
6) Recursively traverse the right nodes.
- DavidDeMar December 21, 2014