Skip to content

Commit 01ae45b

Browse files
committedMar 15, 2025
Add solution #788
1 parent f7e0536 commit 01ae45b

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@
598598
785|[Is Graph Bipartite?](./0785-is-graph-bipartite.js)|Medium|
599599
786|[K-th Smallest Prime Fraction](./0786-k-th-smallest-prime-fraction.js)|Medium|
600600
787|[Cheapest Flights Within K Stops](./0787-cheapest-flights-within-k-stops.js)|Medium|
601+
788|[Rotated Digits](./0788-rotated-digits.js)|Medium|
601602
790|[Domino and Tromino Tiling](./0790-domino-and-tromino-tiling.js)|Medium|
602603
791|[Custom Sort String](./0791-custom-sort-string.js)|Medium|
603604
796|[Rotate String](./0796-rotate-string.js)|Easy|

‎solutions/0788-rotated-digits.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* 788. Rotated Digits
3+
* https://leetcode.com/problems/rotated-digits/
4+
* Difficulty: Medium
5+
*
6+
* An integer x is a good if after rotating each digit individually by 180 degrees, we get a valid
7+
* number that is different from x. Each digit must be rotated - we cannot choose to leave it alone.
8+
*
9+
* A number is valid if each digit remains a digit after rotation. For example:
10+
* - 0, 1, and 8 rotate to themselves,
11+
* - 2 and 5 rotate to each other (in this case they are rotated in a different direction, in other
12+
* words, 2 or 5 gets mirrored),
13+
* - 6 and 9 rotate to each other, and
14+
* - the rest of the numbers do not rotate to any other number and become invalid.
15+
*
16+
* Given an integer n, return the number of good integers in the range [1, n].
17+
*/
18+
19+
/**
20+
* @param {number} n
21+
* @return {number}
22+
*/
23+
var rotatedDigits = function(n) {
24+
let result = 0;
25+
26+
for (let i = 1; i <= n; i++) {
27+
if (verify(i)) {
28+
result++;
29+
}
30+
}
31+
32+
return result;
33+
34+
function verify(num) {
35+
const digits = num.toString().split('');
36+
let result = false;
37+
38+
for (const digit of digits) {
39+
if (digit === '3' || digit === '4' || digit === '7') {
40+
return false;
41+
}
42+
43+
if (digit === '2' || digit === '5' || digit === '6' || digit === '9') {
44+
result = true;
45+
}
46+
}
47+
48+
return result;
49+
}
50+
};

0 commit comments

Comments
 (0)
Please sign in to comment.