题解 | #丑数#
丑数
http://www.nowcoder.com/practice/6aa9e04fc3794f68acf8778237ba065b
class Solution {
public:
int GetUglyNumber_Solution(int index) {
// 动态规划
vector<int> dp(index + 1);
dp[1] = 1;
int p2 = 1;
int p3 = 1;
int p5 = 1;
for(int i = 2; i <= index; i++) {
int num2 = dp[p2] * 2, num3 = dp[p3] * 3, num5 = dp[p5] * 5;
int minN = min(num2, num3);
minN = min(minN, num5);
dp[i] = minN;
// 分别判断当前的最小值与哪一个数相等,排除重复情况
if(minN == num2) {
p2++;
}
if(minN == num3) {
p3++;
}
if(minN == num5){
p5++;
}
}
return dp[index];
}
};