-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotated_digits.cpp
More file actions
108 lines (102 loc) · 2.42 KB
/
rotated_digits.cpp
File metadata and controls
108 lines (102 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* =====================================================================================
*
* Filename: rotated_digits.cpp
*
* Description: 788. Rotated Digits.
* https://leetcode.com/problems/rotated-digits/
*
* Version: 1.0
* Created: 02/19/19 12:29:41
* Revision: none
* Compiler: gcc
*
* Author: Zhu Xianfeng (), xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using std::vector;
// Brute force
class Solution1 {
public:
int rotatedDigits(int num) {
int count = 0;
for (int i = 1; i <= num; i++) {
if (isValidNumber(i)) {
count++;
}
}
return count;
}
private:
bool isValidNumber(int num) {
bool changed = false;
while (num > 0) {
const int unit = num % 10;
if (unit == 2 || unit == 5 || unit == 6 || unit == 9) {
// Value changed
changed = true;
} else if (unit == 3 || unit == 4 || unit == 7) {
// Invalid number
return false;
} else {
; // 0, 1, 8, value not changed
}
num = num / 10;
}
return changed;
}
};
// Dynamic programming
class Solution2 {
public:
int rotatedDigits(int num) {
int count = 0;
vector<int> dp(num + 1);
for (int i = 0; i < (num + 1); i++) {
if (i < 10) {
if (i == 0 || i == 1 || i == 8) {
// Value not changed
dp[i] = 0;
} else if (i == 2 || i == 5 || i == 6 || i == 9) {
// Value changed
dp[i] = 1;
count++;
} else {
// Invalid number
dp[i] = -1;
}
} else {
const int a = i / 10;
const int b = i % 10;
if (dp[a] >= 0 && dp[b] >= 0) {
if (dp[a] == 0 && dp[b] == 0) {
dp[i] = 0;
} else {
dp[i] = 1;
count++;
}
} else {
dp[i] = -1;
}
}
}
return count;
}
};
TEST(Solution, rotatedDigits) {
vector<std::pair<int, int>> cases = {
std::make_pair(1, 0),
std::make_pair(2, 1),
std::make_pair(10, 4),
std::make_pair(857, 247),
};
for (const auto& c : cases) {
EXPECT_EQ(Solution1().rotatedDigits(c.first), c.second);
EXPECT_EQ(Solution2().rotatedDigits(c.first), c.second);
}
}