https://stackoverflow.com/questions/8305199/the-tilde-operator-in-python
LC48:
def is_palindromic(s): return all(s[i] == s[~i] for i in range(len(s) / 2))
class Solution: def rotate(self, A): n = len(A) for i in range(n/2): for j in range(n-n/2): A[i][j], A[~j][i], A[~i][~j], A[j][~i] = \ A[~j][i], A[~i][~j], A[j][~i], A[i][j]LC: 246
class Solution(object): def isStrobogrammatic(self, num): """ :type num: str :rtype: bool 0 0 不能為第一個. 1 1 6 9 9 6 8 8 """ from __builtin__ import xrange dmap = dict((('0', '0'), ('1', '1'), ('6', '9'), ('9', '6'), ('8', '8'))) if num[0] == '0' and len(num) > 1: return False if len(num) % 2: # corner cases if num[len(num) / 2] not in ('0', '1', '8'): return False for i in xrange(len(num) / 2): if num[i] not in dmap or num[~i] != dmap[num[i]]: return False return True
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.