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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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