Saturday, February 24, 2018

LeetCode 788. Rotated Digits - Weekly Contest 73

https://leetcode.com/contest/weekly-contest-73/problems/rotated-digits/

Quite Straight Forward,
in case of 25, 69 they change to something beautiful.
in case of 0, 1, 8, they just don't mean anything,
in case of the 3, 4, 7 they are pretty much deal breaker.

the conditions are messy so I speed them up with an array.



class Solution {
public int rotatedDigits(int N) {
// filter abnormal cases
int count = 0;
this.N = N;
for (int i = 1; i <= N; i++) {
if (isGood(i)) {
count++;
}
}
// return the final result
return count;
}
int N;
int[] change = new int[]{0, 0, 5, -1, -1, 2, 9, -1, 0, 6};
private boolean isGood(int i) {
boolean isChange = false;
while (i > 0) {
int digit = i % 10;
i /= 10;
if (change[digit] == -1) {
return false;
} else if (change[digit] != 0) {
isChange = true;
}
}
return isChange;
}
}

No comments:

Post a Comment