【问题标题】:LeetCode 264. Ugly Number II - custom Ugly Number finding algorithm [duplicate]LeetCode 264. Ugly Number II - 自定义 Ugly Number 查找算法 [重复]
【发布时间】:2020-10-25 06:03:44
【问题描述】:

我正在尝试解决 LeetCode 丑数挑战 II。我想出了自己的算法,似乎在理论上可行,但实际上并没有。我想知道为什么。我用 Java 实现了这个,但我通常用 Python 编写,所以如果您更正 Java 代码,将不胜感激。

问题陈述是:

“写一个程序来找到第n个丑数。

丑数是质因数只包含2的正数, 3、5。

例子:

输入:n = 10 输出:12 解释:1、2、3、4、5、6、8、9、10、12 是前 10 个丑数的序列。注意:

1 通常被视为丑陋的数字。 n 不超过 1690。”

这是我的代码:

class Solution {
    public int nthUglyNumber(int n) {
        // Gather all the uglies in one place
        int[] uglies = new int[n];
        // Set first one as one
        uglies[0] = 1;

        // Start filling
        for (int i = 1; i < uglies.length - 1; i++) {
            int num = i;

            if (isUgly(num) == true) {
                uglies[i] = num;

            } else {
                while (isUgly(num) == false) {
                    num++;
                }

                uglies[i] = num;
                System.out.println(uglies[i]);
            }
        }

        return uglies[uglies.length - 1];
    }
    public boolean isUgly(int m) {
        boolean ans = false;

        // Check if the number is divisible by an integer other than 2,3 or 5
        // Simply iterate through numbers smaller than n to find a smaller divisor
        for (int i = 2; i < m; i++) {
            // If n is divisable by i and i is not a multiple of 2,3 or 5
            boolean other_divisor = (m % i == 0) && (i % 2 != 0 || i % 3 != 0 || i % 5 != 0);

            if (other_divisor == true) {
                ans = false;

            } else {
                ans = true;
            }
        }

        return ans;
    }
}

所以我基本上做了一个函数 isUgly(n),它接受一个数字并通过找出它是否有除数 2、3、5 以外的除数来检查它是否丑陋。如果有,那么它不应该是一个丑陋的数字。比在主体中我检查所有整数并检查它们是否丑陋。如果是,我将它们添加到一个数组中,直到我填写第 n 个位置。如果纠正将不胜感激。

【问题讨论】:

  • 这能回答你的问题吗? nth ugly numberThis question 也引起了一种似曾相识:)
  • 您的 if 语句中不需要 == false== true。您的 isUgly() 方法已经返回一个布尔值。
  • 啊,是的,谢谢。但是算法本身呢?为什么这不起作用?

标签: java arrays number-theory


【解决方案1】:

不确定您的错误在哪里。但是,它必须有效地解决(时间和空间)。

这些解决方案可以通过 LeetCode,但不是解决问题的最有效算法。由于问题与数学有关,因此可以肯定有很多方法可以提高效率。

C++

#include <vector>

class Solution {
public:
    int nthUglyNumber(int n) {
        int factor_two = 0;
        int factor_three = 0;
        int factor_five = 0;

        std::vector<int> uglies(n);
        uglies[0] = 1;

        for (int index = 1; index < n; index++) {
            uglies[index] = std::min(uglies[factor_five] * 5, std::min(uglies[factor_two] * 2, uglies[factor_three] * 3));

            if (uglies[index] == uglies[factor_two] * 2) {
                factor_two++;
            }

            if (uglies[index] == uglies[factor_three] * 3) {
                factor_three++;
            }

            if (uglies[index] == uglies[factor_five] * 5) {
                factor_five++;
            }
        }

        return uglies[n - 1];
    }
};

Java

public class Solution {
    public int nthUglyNumber(int n) {
        int[] uglies = new int[n];
        uglies[0] = 1;
        int indexTwo = 0;
        int indexThree = 0;
        int indexFive = 0;
        int two = 2;
        int three = 3;
        int five = 5;

        for (int index = 1; index < n; index++) {
            int minFactor = Math.min(five, Math.min(two, three));
            uglies[index] = minFactor;

            if (minFactor == two) {
                two = 2 * uglies[++indexTwo];
            }

            if (minFactor == three) {
                three = 3 * uglies[++indexThree];

            }

            if (minFactor == five) {
                five = 5 * uglies[++indexFive];
            }
        }

        return uglies[n - 1];
    }
}

Python

class Solution:
    def nthUglyNumber(self, n: int) -> int:
        uglies = (1,)
        factor_two = factor_three = factor_five = 0
        while len(uglies) < n:
            while uglies[factor_two] * 2 <= uglies[-1]:
                factor_two += 1

            while uglies[factor_three] * 3 <= uglies[-1]:
                factor_three += 1

            while uglies[factor_five] * 5 <= uglies[-1]:
                factor_five += 1

            curr_ugly = min(uglies[factor_two] * 2, uglies[factor_three] * 3, uglies[factor_five] * 5)
            uglies += (curr_ugly,)

        return uglies[-1]

参考文献

  • 有关其他详细信息,您可以查看Discussion Board。有很多公认的解决方案,有各种languages 和解释、高效的算法,以及渐近的time/space 复杂性分析1, 2

如果你正在准备interviews

  • 我们希望根据标准和约定编写bug-freeclean 代码(例如,12@987654334 @、212121111)。总体而言,我们希望避免任何可能在采访中引起争议的事情。

  • 还有其他类似的platforms,您可能需要熟悉它们,以防您要采访将使用这些平台的特定公司。

如果你正在练习contests1

  • 尽可能快地编写代码,几乎所有其他事情都非常琐碎。

  • 对于easy 的问题,蛮力算法通常会被接受。对于采访,蛮力是不太需要的,特别是如果问题是easy 级别。

  • 对于 mediumhard 问题,大约 90% 的情况下,蛮力算法大多会因 Time Limit Exceeded (TLE) 而失败,而较少出现内存限制超出 (MLE) 错误。

  • 根据here 解释的算法对参赛者进行排名。

【讨论】:

  • 您的回答似乎比我在网上看到的更具可读性,谢谢。我知道这是一种无效的算法,但我仍然想要一种不同的方法。
猜你喜欢
  • 2020-10-12
  • 2015-04-23
  • 1970-01-01
  • 2018-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-09
相关资源
最近更新 更多