Created
September 16, 2013 06:12
-
-
Save piecehealth/6577147 to your computer and use it in GitHub Desktop.
腾讯面试题: 给你10分钟时间,根据上排给出十个数,在其下排填出对应的十个数 要求下排每个数都是先前上排那十个数在下排出现的次数。 上排的十个数如下: 【0,1,2,3,4,5,6,7,8,9】 举一个例子, 数值: 0,1,2,3,4,5,6,7,8,9 分配: 6,2,1,0,0,0,1,0,0,0 0在下排出现了6次,1在下排出现了2次, 2在下排出现了1次,3在下排出现了0次.... 以此类推..
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_all_possible_arrs n, total | |
return [[0] * n] if total == 0 | |
return [[total]] if n == 1 | |
possible_arrs = [] | |
0.upto(total) do |i| | |
get_all_possible_arrs(n - 1, total - i).each {|arr| possible_arrs << arr.unshift(i)} | |
end | |
return possible_arrs | |
end | |
def valid? arr | |
arr.each_with_index do |v, i| | |
return false if arr.select {|j| j == i}.size != v | |
end | |
true | |
end | |
get_all_possible_arrs(10, 10).each {|arr| p arr if valid? arr} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment