File tree 2 files changed +51
-0
lines changed
2 files changed +51
-0
lines changed Original file line number Diff line number Diff line change 598
598
785|[ Is Graph Bipartite?] ( ./0785-is-graph-bipartite.js ) |Medium|
599
599
786|[ K-th Smallest Prime Fraction] ( ./0786-k-th-smallest-prime-fraction.js ) |Medium|
600
600
787|[ Cheapest Flights Within K Stops] ( ./0787-cheapest-flights-within-k-stops.js ) |Medium|
601
+ 788|[ Rotated Digits] ( ./0788-rotated-digits.js ) |Medium|
601
602
790|[ Domino and Tromino Tiling] ( ./0790-domino-and-tromino-tiling.js ) |Medium|
602
603
791|[ Custom Sort String] ( ./0791-custom-sort-string.js ) |Medium|
603
604
796|[ Rotate String] ( ./0796-rotate-string.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments