-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFancyNumber.java
More file actions
48 lines (41 loc) · 1.01 KB
/
FancyNumber.java
File metadata and controls
48 lines (41 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package math;
import java.util.HashMap;
/**
* Find if a number is fancy. A number is fancy when it is rotated by 180
* degrees and it still remains the same.
*
* Link: http://www.geeksforgeeks.org/check-if-a-given-number-is-fancy/
*
* @author shivam.maharshi
*/
public class FancyNumber {
private static HashMap<Integer, Integer> fancyMap = new HashMap<>();
private static void init() {
fancyMap.put(0, 0);
fancyMap.put(1, 1);
fancyMap.put(6, 9);
fancyMap.put(8, 8);
fancyMap.put(9, 6);
}
public static boolean isFancy(int num) {
init();
int n = num;
int revNum = 0;
while (n != 0) {
revNum *= 10;
revNum += n % 10;
n = n / 10;
}
System.out.println("RevNum : " + revNum);
while (num != 0) {
if (fancyMap.get(num % 10) == null || fancyMap.get(num % 10) != revNum % 10)
return false;
num /= 10;
revNum /= 10;
}
return true;
}
public static void main(String[] args) {
System.out.println(FancyNumber.isFancy(121));
}
}