LeetCode_丑数

✨✨所属专栏:LeetCode刷题专栏✨✨

✨✨作者主页:嶔某✨✨

题目:

题解:

由题,我们知道丑数大于0,丑数都可以写成2*2*...*2*3*3...*3*5*5...*5,有了这个基础就很好写代码了。

用三个while循环将前面的2 3 5全部除掉如果这个数是丑数,最后n是等于1的,反之n不等于1。

bool isUgly(int n){
    if(n<=0){
        return false;
    }
    if(n==1){
        return true;
    }
    while(n%2==0){
        n/=2;
    }
    while(n%3==0){
        n/=3;
    }
    while(n%5==0){
        n/=5;
    }
    return n==1;
}

此外我们还可以写成递归:

bool isUgly(int n)
{
	if (n == 1)
		return 1;
	if (n == 0)
		return 0;
	if (n % 2 == 0)
		return isUgly(n / 2);
	if (n % 3 == 0)
		return isUgly(n / 3);
	if (n % 5 == 0)
		return isUgly(n / 5);
    return 0;
}

本期博客到这里就结束了,如果有什么错误,欢迎指出,如果对你有帮助,请点个赞,谢谢!

posted @ 2024-04-15 18:27  QinMou~  阅读(5)  评论(0)    收藏  举报  来源