/**
* Problem: LeetCode 788 - Rotated Digits
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int rotatedDigits(int n) {
int[] dp = new int[n + 1];
int count = 0;
for (int i = 0; i <= n; i++) {
if (i < 10) {
if (i == 0 || i == 1 || i == 8) {
dp[i] = 1;
} else if (i == 2 || i == 5 || i == 6 || i == 9) {
dp[i] = 2;
count++;
}
} else {
int a = dp[i / 10];
int b = dp[i % 10];
if (a == 1 && b == 1) {
dp[i] = 1;
} else if (a >= 1 && b >= 1) {
dp[i] = 2;
count++;
}
}
}
return count;
}
}
"""
Problem: LeetCode 788 - Rotated Digits
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
class Solution:
def rotatedDigits(self, n: int) -> int:
dp = [0] * (n + 1)
count = 0
for i in range(n + 1):
if i < 10:
if i in (0, 1, 8):
dp[i] = 1
elif i in (2, 5, 6, 9):
dp[i] = 2
count += 1
else:
a = dp[i // 10]
b = dp[i % 10]
if a == 1 and b == 1:
dp[i] = 1
elif a >= 1 and b >= 1:
dp[i] = 2
count += 1
return count
/**
* Problem: LeetCode 788 - Rotated Digits
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
using namespace std;
/**
* Problem: LeetCode 788 - Rotated Digits
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
int rotatedDigits(int n) {
vector<int> dp(n + 1, 0);
int count = 0;
for (int i = 0; i <= n; i++) {
if (i < 10) {
if (i == 0 || i == 1 || i == 8) {
dp[i] = 1;
} else if (i == 2 || i == 5 || i == 6 || i == 9) {
dp[i] = 2;
count++;
}
} else {
int a = dp[i / 10];
int b = dp[i % 10];
if (a == 1 && b == 1) {
dp[i] = 1;
} else if (a >= 1 && b >= 1) {
dp[i] = 2;
count++;
}
}
}
return count;
}
};
/**
* Problem: LeetCode 788 - Rotated Digits
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
var rotatedDigits = function(n) {
const dp = new Int8Array(n + 1);
let count = 0;
for (let i = 0; i <= n; i++) {
if (i < 10) {
if (i === 0 || i === 1 || i === 8) {
dp[i] = 1;
} else if (i === 2 || i === 5 || i === 6 || i === 9) {
dp[i] = 2;
count++;
}
} else {
const a = dp[Math.floor(i / 10)];
const b = dp[i % 10];
if (a === 1 && b === 1) {
dp[i] = 1;
} else if (a >= 1 && b >= 1) {
dp[i] = 2;
count++;
}
}
}
return count;
};