- Strobogrammatic Number
Strobogrammatic Number
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
Write a function to determine if a number is strobogrammatic. The number is represented as a string.
For example, the numbers “69”, “88”, and “818” are all strobogrammatic.
Solution:
1. public class Solution {
2. public boolean isStrobogrammatic(String num) {
3. if (num == null)
4. return false;
5. 6. return helper(num, 0, num.length() - 1);
7. }
8. 9. boolean helper(String s, int lo, int hi) {
10. if (lo > hi)
11. return true;
12. 13. char c1 = s.charAt(lo);
14. char c2 = s.charAt(hi);
15. 16. int mul = (c1 - '0') * (c2 - '0');
17. 18. if (mul == 1 || mul == 54 || mul == 64 || (mul == 0 && c1 == c2))
19. return helper(s, lo + 1, hi - 1);
20. 21. return false;
22. }
23. }
