Finding all paths down stairs?

后端 未结 13 1382
天命终不由人
天命终不由人 2020-12-13 07:21

I was given the following problem in an interview:

Given a staircase with N steps, you can go up with 1 or 2 steps each time. Output all possible way

13条回答
  •  囚心锁ツ
    2020-12-13 08:08

    Complete C-Sharp code for this

     void PrintAllWays(int n, string str) 
        {
            string str1 = str;
            StringBuilder sb = new StringBuilder(str1);
            if (n == 0) 
            {
                Console.WriteLine(str1);
                return;
            }
            if (n >= 1) 
            {
                sb = new StringBuilder(str1);
                PrintAllWays(n - 1, sb.Append("1").ToString());
            }
            if (n >= 2) 
            {
                sb = new StringBuilder(str1);
                PrintAllWays(n - 2, sb.Append("2").ToString());
            }
        }
    

提交回复
热议问题