Skip to content

Commit 3006e86

Browse files
add confusing-number article (#4972)
* add maximum-distance-in-arrays article * fix article * fix csharp method signature * fix wiggle-sort python heap solution * add confusing-number.md article * Update explanation of L in complexity analysis Clarify the definition of L in terms of n. * Fix logarithm notation in complexity section Updated logarithm notation in complexity explanation. * Fix LaTeX formatting for logarithm in documentation --------- Co-authored-by: neetcode-gh <[email protected]>
1 parent 116898c commit 3006e86

File tree

1 file changed

+252
-0
lines changed

1 file changed

+252
-0
lines changed

articles/confusing-number.md

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
## 1. Invert and Reverse
2+
3+
::tabs-start
4+
5+
```python
6+
class Solution:
7+
def confusingNumber(self, n: int) -> bool:
8+
# Use 'invertMap' to invert each valid digit.
9+
invert_map = {"0":"0", "1":"1", "8":"8", "6":"9", "9":"6"}
10+
rotated_number = []
11+
12+
# Iterate over each digit of 'n'.
13+
for ch in str(n):
14+
if ch not in invert_map:
15+
return False
16+
17+
# Append the inverted digit of 'ch' to the end of 'rotated_number'.
18+
rotated_number.append(invert_map[ch])
19+
20+
rotated_number = "".join(rotated_number)
21+
22+
# Check if the reversed 'rotated_number' equals 'n'.
23+
return int(rotated_number[::-1]) != n
24+
```
25+
26+
```java
27+
class Solution {
28+
public boolean confusingNumber(int n) {
29+
// Use 'invertMap' to invert each valid digit.
30+
Map<Character, Character> invertMap = new HashMap<>() {{
31+
put('0', '0');
32+
put('1', '1');
33+
put('6', '9');
34+
put('8', '8');
35+
put('9', '6');
36+
}};
37+
StringBuilder sb = new StringBuilder();
38+
39+
// Iterate over each digit of 'n'.
40+
for (char ch : String.valueOf(n).toCharArray()) {
41+
if (!invertMap.containsKey(ch)) {
42+
return false;
43+
}
44+
45+
// Append the inverted digit of 'ch' to the end of 'rotatedNumber'.
46+
sb.append(invertMap.get(ch));
47+
}
48+
49+
// Check if the reversed 'rotatedNumber' equals 'n'.
50+
sb.reverse();
51+
return Integer.parseInt(sb.toString()) != n;
52+
}
53+
}
54+
```
55+
56+
```cpp
57+
class Solution {
58+
public:
59+
bool confusingNumber(int n) {
60+
// Use 'invertMap' to invert each valid digit.
61+
unordered_map<char, char> invertMap = {{'0','0'}, {'1','1'}, {'6','9'}, {'8','8'}, {'9','6'}};
62+
string rotatedNumber;
63+
64+
// Iterate over each digit of 'n'.
65+
for (auto ch : to_string(n)) {
66+
if (invertMap.find(ch) == invertMap.end()) {
67+
return false;
68+
}
69+
70+
// Append the inverted digit of 'ch' to the end of 'rotatedNumber'.
71+
rotatedNumber += invertMap[ch];
72+
}
73+
74+
// Check if the reversed 'rotatedNumber' equals 'n'.
75+
reverse(begin(rotatedNumber), end(rotatedNumber));
76+
return stoi(rotatedNumber) != n;
77+
}
78+
};
79+
```
80+
81+
```javascript
82+
class Solution {
83+
/**
84+
* @param {number} n
85+
* @return {boolean}
86+
*/
87+
confusingNumber(n) {
88+
// Use 'invertMap' to invert each valid digit.
89+
const invertMap = {
90+
'0': '0',
91+
'1': '1',
92+
'6': '9',
93+
'8': '8',
94+
'9': '6'
95+
};
96+
97+
let rotatedNumber = '';
98+
// Iterate over each digit of 'n'.
99+
for (const ch of String(n)) {
100+
if (!(ch in invertMap)) {
101+
return false;
102+
}
103+
// Append the inverted digit of 'ch' to the end of 'rotatedNumber'.
104+
rotatedNumber += invertMap[ch];
105+
}
106+
// Check if the reversed 'rotatedNumber' equals 'n'.
107+
rotatedNumber = rotatedNumber.split('').reverse().join('');
108+
return parseInt(rotatedNumber) !== n;
109+
}
110+
}
111+
```
112+
113+
::tabs-end
114+
115+
### Time & Space Complexity
116+
117+
- Time complexity: $O(L)$
118+
- Space complexity: $O(L)$ extra space used
119+
120+
> Where $L$ is the maximum number of digits $n$ can have ($L = \log_{10} n$).
121+
122+
---
123+
124+
## 2. Use the remainder
125+
126+
::tabs-start
127+
128+
```python
129+
class Solution:
130+
def confusingNumber(self, n: int) -> bool:
131+
# Use 'invert_map' to invert each valid digit. Since we don't want to modify
132+
# 'n', we create a copy of it as 'nCopy'.
133+
invert_map = {0:0, 1:1, 8:8, 6:9, 9:6}
134+
invert_number = 0
135+
n_copy = n
136+
137+
# Get every digit of 'n_copy' by taking the remainder of it to 10.
138+
while n_copy:
139+
res = n_copy % 10
140+
if res not in invert_map:
141+
return False
142+
143+
# Append the inverted digit of 'res' to the end of 'rotated_number'.
144+
invert_number = invert_number * 10 + invert_map[res]
145+
n_copy //= 10
146+
147+
# Check if 'rotated_number' equals 'n'.
148+
return invert_number != n
149+
```
150+
151+
```java
152+
class Solution {
153+
public boolean confusingNumber(int n) {
154+
// Use 'invertMap' to invert each valid digit. Since we don't want to modify
155+
// 'n', we create a copy of it as 'nCopy'.
156+
Map<Integer, Integer> invertMap = new HashMap<>() {{
157+
put(0, 0);
158+
put(1, 1);
159+
put(6, 9);
160+
put(8, 8);
161+
put(9, 6);
162+
}};
163+
int nCopy = n, rotatedNumber = 0;
164+
165+
// Get every digit of 'nCopy' by taking the remainder of it to 10.
166+
while (nCopy > 0) {
167+
int res = nCopy % 10;
168+
if (!invertMap.containsKey(res)) {
169+
return false;
170+
}
171+
172+
// Append the inverted digit of 'res' to the end of 'rotatedNumber'.
173+
rotatedNumber = rotatedNumber * 10 + invertMap.get(res);
174+
nCopy /= 10;
175+
}
176+
177+
// Check if 'rotatedNumber' equals 'n'.
178+
return rotatedNumber != n;
179+
}
180+
}
181+
```
182+
183+
```cpp
184+
class Solution {
185+
public:
186+
bool confusingNumber(int n) {
187+
// Use 'invertMap' to invert each valid digit. Since we don't want to modify
188+
// 'n', we create a copy of it as 'nCopy'.
189+
map<int, int> invertMap = {{0, 0}, {1, 1}, {6, 9}, {8, 8}, {9, 6}};
190+
int rotatedNumber = 0, nCopy = n;
191+
192+
// Get every digit of 'nCopy' by taking the remainder of it to 10.
193+
while (nCopy > 0) {
194+
int res = nCopy % 10;
195+
if (invertMap.find(res) == invertMap.end()) {
196+
return false;
197+
}
198+
199+
// Append the inverted digit of 'res' to the end of 'rotatedNumber'.
200+
rotatedNumber = rotatedNumber * 10 + invertMap[res];
201+
nCopy /= 10;
202+
}
203+
204+
// Check if 'rotatedNumber' equals 'n'.
205+
return rotatedNumber != n;
206+
}
207+
};
208+
```
209+
210+
```javascript
211+
class Solution {
212+
/**
213+
* @param {number} n
214+
* @return {boolean}
215+
*/
216+
confusingNumber(n) {
217+
// Use 'invertMap' to invert each valid digit. Since we don't want to modify
218+
// 'n', we create a copy of it as 'nCopy'.
219+
const invertMap = {
220+
0: 0,
221+
1: 1,
222+
6: 9,
223+
8: 8,
224+
9: 6
225+
};
226+
let nCopy = n;
227+
let rotatedNumber = 0;
228+
229+
// Get every digit of 'nCopy' by taking the remainder of it to 10.
230+
while (nCopy > 0) {
231+
let res = nCopy % 10;
232+
if (!(res in invertMap)) {
233+
return false;
234+
}
235+
// Append the inverted digit of 'res' to the end of 'rotatedNumber'.
236+
rotatedNumber = rotatedNumber * 10 + invertMap[res];
237+
nCopy = Math.floor(nCopy / 10);
238+
}
239+
// Check if 'rotatedNumber' equals 'n'.
240+
return rotatedNumber !== n;
241+
}
242+
}
243+
```
244+
245+
::tabs-end
246+
247+
### Time & Space Complexity
248+
249+
- Time complexity: $O(L)$
250+
- Space complexity: $O(L)$ extra space used
251+
252+
> Where $L$ is the maximum number of digits $n$ can have.

0 commit comments

Comments
 (0)