I got a code but it's C++ which I don't know. But there are many similarities between C++ and Java in my opinion. The only things I don't know what they mean / how to write them in Java are these:
u = getNextUglyNumber(twoQ, threeQ, fiveQ, &q); //and what is &q, the &?
twoQ.push(u << 1); //what is << ?
std::cout << u << ' '; //this i dont understand at all
*q = 2; // same as q = 2*q?
if (fiveQ.front() < u) {
u = fiveQ.front(); //whats front?
Thanks a lot in advance for any kind of help!
Here is also the full code:
typedef std::queue<int> Queue;
int findNthUglyNumber(int n) {
Queue twoQ, threeQ, fiveQ;
twoQ.push(2);
threeQ.push(3);
fiveQ.push(5);
int u, q;
while (n) {
u = getNextUglyNumber(twoQ, threeQ, fiveQ, &q);
switch (q) {
case 2:
twoQ.push(u << 1); /// u * 2
case 3:
threeQ.push(u << 2 - u); /// u * 3
case 5:
fiveQ.push(u << 2 + u); /// u * 5
}
n--;
std::cout << u << ' ';
}
return u;
}
int getNextUglyNumber(Queue &twoQ, Queue &threeQ, Queue &fiveQ, int &q) {
int u = twoQ.front();
*q = 2;
if (threeQ.front() < u) {
u = threeQ.front();
*q = 3;
}
if (fiveQ.front() < u) {
u = fiveQ.front();
*q = 5;
}
switch (*q) {
case 2:
twoQ.pop();
break;
case 3:
threeQ.pop();
break;
case 5:
fiveQ.pop();
break;
}
return u;
}