Skip to content

Commit 3ee64c3

Browse files
add 1056
1 parent 8ddf26d commit 3ee64c3

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Your ideas/fixes/algorithms are more than welcome!
2828
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
3030
|1065|[Index Pairs of a String](https://leetcode.com/problems/index-pairs-of-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1065.java) | O(nlogn) | O(1) | |Medium||
31+
|1056|[Confusing Number](https://leetcode.com/problems/confusing-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1056.java) | O(n) | O(1) | |Easy||
3132
|1055|[Fixed Point](https://leetcode.com/problems/fixed-point/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1055.java) | O(n) | O(1) | |Easy||
3233
|1051|[Height Checker](https://leetcode.com/problems/height-checker/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1051.java) | O(nlogn) | O(1) | |Easy||
3334
|1047|[Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1047.java) | O(n) | O(1) | |Easy||
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* 1056. Confusing Number
8+
*
9+
* Given a number N, return true if and only if it is a confusing number, which satisfies the following condition:
10+
* We can rotate digits by 180 degrees to form new digits. When 0, 1, 6, 8, 9 are rotated 180 degrees,
11+
* they become 0, 1, 9, 8, 6 respectively. When 2, 3, 4, 5 and 7 are rotated 180 degrees,
12+
* they become invalid. A confusing number is a number that when rotated 180 degrees becomes a different number with each digit valid.
13+
*
14+
* Example 1:
15+
* Input: 6
16+
* Output: true
17+
* Explanation:
18+
* We get 9 after rotating 6, 9 is a valid number and 9!=6.
19+
*
20+
*
21+
* Example 2:
22+
* Input: 89
23+
* Output: true
24+
* Explanation:
25+
* We get 68 after rotating 89, 86 is a valid number and 86!=89.
26+
*
27+
*
28+
* Example 3:
29+
* Input: 11
30+
* Output: false
31+
* Explanation:
32+
* We get 11 after rotating 11, 11 is a valid number but the value remains the same, thus 11 is not a confusing number.
33+
*
34+
* Example 4:
35+
* Input: 25
36+
* Output: false
37+
* Explanation:
38+
* We get an invalid number after rotating 25.
39+
*
40+
* Note:
41+
*
42+
* 0 <= N <= 10^9
43+
* After the rotation we can ignore leading zeros, for example if after rotation we have 0008 then this number is considered as just 8.*/
44+
public class _1056 {
45+
public static class Solution1 {
46+
Map<Integer, Integer> map = new HashMap<Integer, Integer>() {{
47+
put(0, 0);
48+
put(1, 1);
49+
put(8, 8);
50+
put(6, 9);
51+
put(9, 6);
52+
}};
53+
54+
public boolean confusingNumber(int N) {
55+
if (N == 0) {
56+
return false;
57+
}
58+
int newNumber = 0;
59+
int originalN = N;
60+
while (N != 0) {
61+
newNumber *= 10;
62+
int digit = N % 10;
63+
if (!map.containsKey(digit)) {
64+
return false;
65+
}
66+
digit = map.get(digit);
67+
newNumber += digit;
68+
N /= 10;
69+
}
70+
return newNumber != originalN;
71+
}
72+
}
73+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1056;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1056Test {
10+
private static _1056.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1056.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(true, solution1.confusingNumber(6));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(true, solution1.confusingNumber(89));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(false, solution1.confusingNumber(11));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(false, solution1.confusingNumber(25));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)