1*2(t2) 2*2 3*2
(t3)1*3 2*3 3*3
(t5)1*5 2*5 3*5
1*2 2*2(t2) 3*2
1*3(t3) 2*3 3*3
(t5)1*5 2*5 3*5
Solution:
public int nthUglyNumber(int n) {
if(n == 1) return 1;
int t2 = 0, t3 = 0, t5 = 0;
int[] k = new int[n];
k[0] = 1;
for(int i = 1; i < n; i++) {
k[i] = Math.min(Math.min(k[t2] * 2, k[t3] * 3), k[t5] * 5);
if (k[i] == k[t2] * 2) t2++;
if (k[i] == k[t3] * 3) t3++;
if (k[i] == k[t5] * 5) t5++;
}
return k[n-1];
}
Subscribe to:
Post Comments (Atom)
4. Ugly Number
1*2(t2) 2*2 3*2 (t3)1*3 2*3 3*3 (t5)1*5 2*5 3*5 1*2 2*2(t2) 3*2 1*3(t3) 2*3 3*3 (t5)1*5 2*5 3*5 Solution: public int nthUglyNumbe...
-
Given a list, rotate the list to the right by k places, where k is non-negative. Wrong solution 1: public ListNode rotateRight(ListNo...
-
if (head == null || head . next == null ) { return head; } ListNode lessDummy = new ListNode (...
-
1*2(t2) 2*2 3*2 (t3)1*3 2*3 3*3 (t5)1*5 2*5 3*5 1*2 2*2(t2) 3*2 1*3(t3) 2*3 3*3 (t5)1*5 2*5 3*5 Solution: public int nthUglyNumbe...
No comments:
Post a Comment