盛世宏图 发表于 2024-7-23 08:51:41

Golang | Leetcode Golang题解之第264题丑数II

标题:
https://i-blog.csdnimg.cn/direct/8e9aa7e943db4611b6e91d971e6bfe7e.png
题解:
func nthUglyNumber(n int) int {
    dp := make([]int, n+1)
    dp = 1
    p2, p3, p5 := 1, 1, 1
    for i := 2; i <= n; i++ {
      x2, x3, x5 := dp*2, dp*3, dp*5
      dp = min(min(x2, x3), x5)
      if dp == x2 {
            p2++
      }
      if dp == x3 {
            p3++
      }
      if dp == x5 {
            p5++
      }
    }
    return dp
}

func min(a, b int) int {
    if a < b {
      return a
    }
    return b
}
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Golang | Leetcode Golang题解之第264题丑数II