简单题

Ugly Number

My Submissions

Question
Total Accepted: 32635 Total Submissions: 94009 Difficulty: Easy

Write a program to check whether a given number is an ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.

Note that 1 is typically treated as an ugly number.

Discuss中总有惊喜:

Java:

 for (int i=2; i<6 && num>0; i++)
while (num % i == 0)
num /= i;
return num == 1;

Python:

 for p in 2, 3, 5:
while num % p == 0 < num:
num /= p
return num == 1

C++:(其中&&之后的num怎么理解?)

 for (int i=; i< && num; i++)
while (num % i == )
num /= i;
return num == ;

自己的:

C++:

 class Solution {
public:
bool isUgly(int num) {
if (num <= )
return false;
if (num == )
return true; while (num != ) {
if (num % == ) {
num = num / ;
}
else if (num % == ) {
num = num / ;
}
else if (num % == ) {
num = num / ;
}
else{
return false;
}
}
return true;
}
};

【13_263】Ugly Number的更多相关文章

  1. 【POJ2104】【HDU2665】K-th Number 主席树

    [POJ2104][HDU2665]K-th Number Description You are working for Macrohard company in data structures d ...

  2. 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)

    [SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...

  3. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  4. 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)

    [LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  5. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  6. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  7. 【HDU3948】 The Number of Palindromes (后缀数组+RMQ)

    The Number of Palindromes Problem Description Now, you are given a string S. We want to know how man ...

  8. 【leetcode78】Single Number II

    题目描述: 给定一个数组,里面除了一个数字,其他的都出现三次.求出这个数字 原文描述: Given an array of integers, every element appears three ...

  9. 【u020】Couple number

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 任何一个整数N都能表示成另外两个整数a和b的平方差吗?如果能,那么这个数N就叫做Couple numb ...

随机推荐

  1. C++对象模型

    1.类布局 1.1简单类对象的内存布局 class A { public: void f(); private: int i; char c; static int s; }; 简单对象的内存布局:非 ...

  2. LDAP客户端

    LDAP客户端通过与服务端关联起来,就可以使用服务端的系统账号登录系统,通过useradd 添加用户是在ldap里是没有显示的,ldap添加用户,在/etc/passwd里也是没有显示的,ldap添加 ...

  3. CentOS 6.5 下离线安装nginx

    一.由于linux下安装nginx 需要  zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc这些依赖,而这些依赖打安装包在我们系统的光 ...

  4. HTML5 ---localStorage

    HTML5中提供了localStorage对象可以将数据长期保存在客户端,直到人为清除. localStorage提供了几个方法: 1.存储:localStorage.setItem(key,valu ...

  5. web程序员该学习什么

    以我个人的观点分了几个级别,仅供参考 初级发展(学习期) 前端应该学习HTML javascript css 能够制造简单的前端页面满足自己的工作需求 后端应该学习asp.net or jsp or ...

  6. 关闭Win10自带的 Windows Defender

    1.按下Win+R,输入gpedit.msc 2.进入组策略,选择计算机配置>管理模板>Windows 组件>Windows Defender 3.双击"关闭 Window ...

  7. Nginx-->基础-->理论-->001:Nginx基本介绍

    一.nginx基本介绍 传统上基于进程或者线程模型架构的web服务通过每进程或者每线程处理并发连接请求,这势必毁在网络和I/O操作时产生阻塞,其另外一个必然结果则是对内存和CPU的利用率低下,产生一个 ...

  8. soapUI 时间格式

    用soapUI测试webservice,接收DateTime格式,总是包 not a valid AllXsd value 老外说,必须用ISO8601格式,如: 2009-03-13T22:16:0 ...

  9. 深入理解Java虚拟机(二)、Java对象的创建,内存布局和访问定位

    对象的创建: Object obj = new Object(); 常量池中是否有Ljava.lang.Object

  10. flask-admin章节三:数据库迁移工具 alembic初步使用

    1. 概述 基于flask框架构建web,一般会使用sqlchemy(在flask中使用sqlchemy可以参考这里)作为数据库引擎. 这样业务的逻辑就可以做到不跟具体的数据库类型相耦合,具体后端业务 ...