Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3ee64c3

Browse files
add 1056
1 parent8ddf26d commit3ee64c3

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+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.HashMap;
4+
importjava.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+
publicclass_1056 {
45+
publicstaticclassSolution1 {
46+
Map<Integer,Integer>map =newHashMap<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+
publicbooleanconfusingNumber(intN) {
55+
if (N ==0) {
56+
returnfalse;
57+
}
58+
intnewNumber =0;
59+
intoriginalN =N;
60+
while (N !=0) {
61+
newNumber *=10;
62+
intdigit =N %10;
63+
if (!map.containsKey(digit)) {
64+
returnfalse;
65+
}
66+
digit =map.get(digit);
67+
newNumber +=digit;
68+
N /=10;
69+
}
70+
returnnewNumber !=originalN;
71+
}
72+
}
73+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1056;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1056Test {
10+
privatestatic_1056.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_1056.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(true,solution1.confusingNumber(6));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals(true,solution1.confusingNumber(89));
25+
}
26+
27+
@Test
28+
publicvoidtest3() {
29+
assertEquals(false,solution1.confusingNumber(11));
30+
}
31+
32+
@Test
33+
publicvoidtest4() {
34+
assertEquals(false,solution1.confusingNumber(25));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp