本文主要是介绍lintcode Ugly Number II python,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Description
Ugly number is a number that only have factors 2
, 3
and 5
.
Design an algorithm to find the nth ugly number. The first 10 ugly numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12...
利用动态规划的思想
def find_n_ulgy_number(n):count = 1num = 1res = set([1,2,3,5])while count<n:num += 1if num in res:res.add(num*2)res.add(num*3)res.add(num*5)count += 1return numprint find_n_ulgy_number(19)
这篇关于lintcode Ugly Number II python的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!