丑数(剑指offer)---C++实现

《参考程序员面试金典》伪代码如下
1)初始化array和队列:Q2 Q3 Q5

2)将1插入array

3)分别将12、13 、1*5插入Q2 Q3 Q5

4)令x为Q2 Q3 Q5中的最小值,将x添加至array尾部

5)若x存在于:
Q2:将 x * 2、x * 3、x5 分别放入Q2 Q3 Q5,从Q2中移除x
Q3:将 x * 3、x
5 分别放入Q3 Q5,从Q3中移除x
Q5:将 x * 5放入Q5,从Q5中移除x

long long getMinNumInThreeNums(long long a, long long b, long long c) {
    	long long t = min(a, b);
    	return min(t, c);
    }
    
    long long GetUglyN

umber_Solution(int index) {
    	if (index) {
    		if (index == 1) {
    			return 1;
    		}
    		priority_queue, greater >q2;
    		priority_queue, greater >q3;
    		priority_queue, greater >q5;
    		q2.push(2);
    		q3.push(3);
    		q5.push(5);
    		long long flg, minNum;
    		flg = 0;
    		while (flg < index - 1) {
    			minNum = getMinNumInThreeNums(q2.top(), q3.top(), q5.top());
    			if (minNum == q2.top()) {
    				q2.pop();
    				q2.push(minNum * 2);
    				q3.push(minNum * 3);
    				q5.push(minNum * 5);
    			}
    			else if (minNum == q3.top()) {
    				q3.pop();
    				q3.push(minNum * 3);
    				q5.push(minNum * 5);
    			}
    			else {
    				q5.pop();
    				q5.push(minNum * 5);
    			}
    			flg++;
    		}
    		return minNum;
    	}
    	return -1;
    }

你可能感兴趣的:(c/c++面试,数据结构,算法题)