Related:
LeetCode 246 - Strobogrammatic Number
LeetCode 247 - Strobogrammatic Number II
LeetCode 248 - Strobogrammatic Number III
http://sbzhouhao.net/LeetCode/LeetCode-Strobogrammatic-Number.html
https://discuss.leetcode.com/topic/20837/4-lines-in-java
https://discuss.leetcode.com/topic/21576/accepted-java-solution
https://discuss.leetcode.com/topic/20729/ac-clean-java-recursion-solution
http://www.geeksforgeeks.org/check-if-a-given-number-is-fancy/
https://en.wikipedia.org/wiki/Strobogrammatic_number
LeetCode 246 - Strobogrammatic Number
LeetCode 247 - Strobogrammatic Number II
LeetCode 248 - Strobogrammatic Number III
http://sbzhouhao.net/LeetCode/LeetCode-Strobogrammatic-Number.html
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.public boolean isStrobogrammatic(String num) { for (int i = 0; i <= num.length() / 2; i++) { char a = num.charAt(i); char b = num.charAt(num.length() - 1 - i); if (!isValid(a, b)) { return false; } } return true; } private boolean isValid(char c, char b) { switch (c) { case '1': return b == '1'; case '6': return b == '9'; case '9': return b == '6'; case '8': return b == '8'; case '0': return b == '0'; default: return false; } }
https://discuss.leetcode.com/topic/20837/4-lines-in-java
public boolean isStrobogrammatic(String num) {
for (int i=0, j=num.length()-1; i <= j; i++, j--)
if (!"00 11 88 696".contains(num.charAt(i) + "" + num.charAt(j)))
return false;
return true;
}
https://discuss.leetcode.com/topic/21576/accepted-java-solution
public boolean isStrobogrammatic(String num) {
Map<Character, Character> map = new HashMap<Character, Character>();
map.put('6', '9');
map.put('9', '6');
map.put('0', '0');
map.put('1', '1');
map.put('8', '8');
int l = 0, r = num.length() - 1;
while (l <= r) {
if (!map.containsKey(num.charAt(l))) return false;
if (map.get(num.charAt(l)) != num.charAt(r))
return false;
l++;
r--;
}
return true;
}
public boolean isStrobogrammatic(String num) {
HashMap map = new HashMap();
map.put('0', '0');
map.put('1', '1');
map.put('6', '9');
map.put('8', '8');
map.put('9', '6');
for (int i = 0; i < num.length(); i++) {
if (!map.containsKey(num.charAt(i)) || (char)map.get(num.charAt(i)) != num.charAt(num.length() - i - 1)) {
return false;
}
}
return true;
}
http://www.tangjikai.com/algorithms/leetcode-246-strobogrammatic-numberdef isStrobogrammatic(self, num):
dic = {'9':'6', '6':'9', '1':'1', '8':'8', '0':'0'}
l, r = 0, len(num) - 1
while l <= r:
if num[l] not in dic or dic[num[l]] != num[r]:
return False
l += 1
r -= 1
return True
https://discuss.leetcode.com/topic/20729/ac-clean-java-recursion-solution
public boolean isStrobogrammatic(String num) {
if (num == null)
return false;
return helper(num, 0, num.length() - 1);
}
boolean helper(String num, int lo, int hi) {
if (lo > hi)
return true;
char c1 = num.charAt(lo), c2 = num.charAt(hi);
int mul = (c1 - '0') * (c2 - '0');
if (mul == 1 || mul == 64 || mul == 54 || (mul == 0 && c1 == c2))
return helper(num, lo + 1, hi - 1);
else
return false;
}
http://www.geeksforgeeks.org/check-if-a-given-number-is-fancy/
The idea is to create a map to store fancy pair mappings. After creating map, traverse given number from both ends and if at any point characters at current ends are not fancy pairs, return false. This algorithm is similar to palindrome check algorithm.
bool
isFancy(string& num)
{
// To store mappings of fancy pair characters. For example
// 6 is paired with 9 and 9 is paired with 6.
map<
char
,
char
> fp;
fp[
'0'
] =
'0'
;
fp[
'1'
] =
'1'
;
fp[
'6'
] =
'9'
;
fp[
'8'
] =
'8'
;
fp[
'9'
] =
'6'
;
// Find number of digits in given number
int
n = num.length();
// Traverse from both ends, and compare characters one by one
int
l = 0, r = n-1;
while
(l<=r)
{
// If current characters at both ends are not fancy pairs
if
(fp.find(num[l]) == fp.end() || fp[num[l]] != num[r])
return
false
;
l++;
r--;
}
return
true
;
}
https://en.wikipedia.org/wiki/Strobogrammatic_number
A strobogrammatic number is a number that, given a base and given a set of glyphs, appears the same whether viewed normally or upside down by rotation of 180 degrees.
In base 10, a legible set of glyphs can be developed where 0, 1 and 8 are symmetrical around the horizontal axis, and 6 and 9 are the same as each other when rotated 180 degrees (such as the digit characters in ASCII using the font Stylus BT). In such a system, the first few strobogrammatic numbers are:
0, 1, 8, 11, 69, 88, 96, 101, 111, 181, 609, 619, 689, 808, 818, 888, 906, 916, 986, 1001 (sequence A000787 in OEIS)
Read full article from LIKE CODING: LeetCode [246] Strobogrammatic Number