Class Solution

java.lang.Object
g0701_0800.s0788_rotated_digits.Solution

public class Solution extends Object
788 - Rotated Digits.<p>Medium</p> <p>An integer <code>x</code> is a <strong>good</strong> if after rotating each digit individually by 180 degrees, we get a valid number that is different from <code>x</code>. Each digit must be rotated - we cannot choose to leave it alone.</p> <p>A number is valid if each digit remains a digit after rotation. For example:</p> <ul> <li><code>0</code>, <code>1</code>, and <code>8</code> rotate to themselves,</li> <li><code>2</code> and <code>5</code> rotate to each other (in this case they are rotated in a different direction, in other words, <code>2</code> or <code>5</code> gets mirrored),</li> <li><code>6</code> and <code>9</code> rotate to each other, and</li> <li>the rest of the numbers do not rotate to any other number and become invalid.</li> </ul> <p>Given an integer <code>n</code>, return <em>the number of <strong>good</strong> integers in the range</em> <code>[1, n]</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> n = 10</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong></p> <pre><code> There are four good numbers in the range [1, 10] : 2, 5, 6, 9. Note that 1 and 10 are not good numbers, since they remain unchanged after rotating. </code></pre> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> n = 1</p> <p><strong>Output:</strong> 0</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> n = 2</p> <p><strong>Output:</strong> 1</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= n <= 10<sup>4</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • rotatedDigits

      public int rotatedDigits(int n)