JiaMing

# 题目: 263. Ugly Number

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`.

Example 1:

```Input: 6
Output: true
Explanation: 6 = 2 × 3```

Example 2:

```Input: 8
Output: true
Explanation: 8 = 2 × 2 × 2```

Example 3:

```Input: 14
Output: false
Explanation: `14` is not ugly since it includes another prime factor `7`.```

Note:

1. `1` is typically treated as an ugly number.
2. Input is within the 32-bit signed integer range: [−231,  231 − 1].

# 最终实现

#### Java实现

``````import java.util.ArrayList;
import java.util.List;

public class Solution {

private List<Long> uglyNums = new ArrayList<>();

/**
*
* The solution for the old problem 263
* @param k the input of the problem
* @return the kth ugly number
*/
public long calcUglyNumber(int k) {
uglyNums.clear();
FactorRecord factorTwo = new FactorRecord(2, 0);
FactorRecord factorThree = new FactorRecord(3, 0);
FactorRecord factorFive = new FactorRecord(5, 0);
for (int i = 0; i < k; i++) {
long minVal = Math.min(factorFive.calcCurrentVal(uglyNums),
Math.min(factorTwo.calcCurrentVal(uglyNums), factorThree.calcCurrentVal(uglyNums)));
if (minVal == factorTwo.calcCurrentVal(uglyNums)) {
if (uglyNums.get(uglyNums.size() - 1) != minVal) {
}
factorTwo.setIndex(factorTwo.getIndex() + 1);
}
if (minVal == factorThree.calcCurrentVal(uglyNums)) {
if (uglyNums.get(uglyNums.size() - 1) != minVal) {
}
factorThree.setIndex(factorThree.getIndex() + 1);
}
if (minVal == factorFive.calcCurrentVal(uglyNums)) {
if (uglyNums.get(uglyNums.size() - 1) != minVal) {
}
factorFive.setIndex(factorFive.getIndex() + 1);
}
}
return uglyNums.get(k);
}

public boolean isUgly(int num) {
if (num == 1) {
return true;
}
uglyNums.clear();
FactorRecord factorTwo = new FactorRecord(2, 0);
FactorRecord factorThree = new FactorRecord(3, 0);
FactorRecord factorFive = new FactorRecord(5, 0);
int k = num;
for (int i = 0; i < k; i++) {
long minVal = Math.min(factorFive.calcCurrentVal(uglyNums),
Math.min(factorTwo.calcCurrentVal(uglyNums), factorThree.calcCurrentVal(uglyNums)));
if (minVal == num) {
return true;
}
if (minVal > num) {
return false;
}
if (minVal == factorTwo.calcCurrentVal(uglyNums)) {
if (uglyNums.get(uglyNums.size() - 1) != minVal) {
}
factorTwo.setIndex(factorTwo.getIndex() + 1);
}
if (minVal == factorThree.calcCurrentVal(uglyNums)) {
if (uglyNums.get(uglyNums.size() - 1) != minVal) {
}
factorThree.setIndex(factorThree.getIndex() + 1);
}
if (minVal == factorFive.calcCurrentVal(uglyNums)) {
if (uglyNums.get(uglyNums.size() - 1) != minVal) {
}
factorFive.setIndex(factorFive.getIndex() + 1);
}
}
return false;
}

class FactorRecord {
long factor; // 2, 3, 5
int index;

public FactorRecord(long factor, int index) {
this.factor = factor;
this.index = index;
}

public void setFactor(long factor) {
this.factor = factor;
}

public void setIndex(int index) {
this.index = index;
}

public long getFactor() {
return factor;
}

public int getIndex() {
return index;
}

public long calcCurrentVal(List<Long> uglyNums) {
return factor * uglyNums.get(index);
}
}

public static void main(String[] args) {
Solution solution = new Solution();
boolean res = solution.isUgly(2144843814);
System.out.println(res);
}

}
``````

### JiaMing

CDH5: 使用parcels配置lzo

cloud-coder
2014/07/01
6.8K
1
jquery通过a标签删除table中的一行的代码

linghangp
2013/12/03
426
0
python 是什么？

Python（英国发音：/?pa?θ?n/ 美国发音：/?pa?θɑ?n）是什么呢？简单的说，它是一种计算机编程语言及一组配套的软件工具和库。是一种面向对象、解释型计算机程序设计语言，由Guido van...

2017/05/24
101
0

2017/06/19
115
0
GreenDAO3的使用教程

gongcheng
2016/08/20
363
0

PHP实现RabbitMQ消息队列

PHP圈子
30分钟前
22
0
pdd笔试题

osc_tylqml9v
30分钟前
0
0

/** * 拓扑排序算法，拓扑都是有向无环图 * 使用场景：编译的时候，比如，springboot启动的时候要读取docker系统环境变量，还要读取各配置文件按照顺序 * 还有比如，a的包依赖...

osc_94gn551r
32分钟前
0
0

32分钟前
12
0

1、长链 https://chelun.eclicks.cn/web/information?info_tid=156984 - 文章test http://cjjl-h5-test.chelun.com/2020/big/index.html - 以小博大test 2、scheme : 钱包 supercoach://myw......

osc_hwc3munb
33分钟前
0
0