1
1
package com .thealgorithms .maths ;
2
2
3
- import java .lang .IllegalArgumentException ;
4
3
import java .util .ArrayList ;
5
4
import java .util .Arrays ;
6
5
import java .util .HashMap ;
16
15
* - the base [2, 3, 5] ugly numbers are the same as base [5, 6, 2, 3, 5] ugly numbers
17
16
*/
18
17
public class NthUglyNumber {
19
- ArrayList <Long > uglyNumbers = new ArrayList <>(Arrays .asList (1L ));
20
- final int [] baseNumbers ;
21
- HashMap <Integer , Integer > positions = new HashMap <>();
18
+ private ArrayList <Long > uglyNumbers = new ArrayList <>(Arrays .asList (1L ));
19
+ private final int [] baseNumbers ;
20
+ private HashMap <Integer , Integer > positions = new HashMap <>();
22
21
23
22
/**
24
23
* @brief initialized the object allowing to compute ugly numbers with given base
25
24
* @param baseNumbers the given base of ugly numbers
26
25
* @exception IllegalArgumentException baseNumber is empty
27
26
*/
28
- NthUglyNumber (int [] baseNumbers ) {
27
+ NthUglyNumber (final int [] baseNumbers ) {
29
28
if (baseNumbers .length == 0 ) {
30
29
throw new IllegalArgumentException ("baseNumbers must be non-empty." );
31
30
}
@@ -41,7 +40,7 @@ public class NthUglyNumber {
41
40
* @exception IllegalArgumentException n is negative
42
41
* @return the n-th ugly number (starting from index 0)
43
42
*/
44
- public Long get (int n ) {
43
+ public Long get (final int n ) {
45
44
if (n < 0 ) {
46
45
throw new IllegalArgumentException ("n must be non-negative." );
47
46
}
@@ -67,7 +66,7 @@ private void updatePositions() {
67
66
}
68
67
}
69
68
70
- private long computeCandidate (int candidateBase ) {
69
+ private long computeCandidate (final int candidateBase ) {
71
70
return candidateBase * uglyNumbers .get (positions .get (candidateBase ));
72
71
}
73
72
0 commit comments