GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/python/351_Android_Unlock_Patterns.py at master · qiyuangong/leetcode Expand file tree/ Copy path351_Android_Unlock_Patterns.pyMore file actions 43 lines (41 loc) · 1.2 KB / Copy path351_Android_Unlock_Patterns.pyFile metadata and controls43 lines (41 loc) · 1.2 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 class Solution(object): def numberOfPatterns(self, m, n): """ :type m: int :type n: int :rtype: int """ used = [False] * 9 res = 0 for l in range(m, n + 1): res += self.calc_patterns(used, -1, l) used = [False] * 9 return res def is_valid(self, used, index, last): # markded if used[index]: return False # first digit if last == -1: return True # adjacent cells (in a row or in a column) if (last + index) % 2 == 1: return True mid = (last + index) / 2 if mid == 4: return used[mid] # adjacent cells on diagonal if (index % 3 != last % 3) and (index / 3 != last / 3): return True # all other cells which are not adjacent return used[mid] def calc_patterns(self, used, last, length): if length == 0: return 1 res = 0 for i in range(9): if self.is_valid(used, i, last): used[i] = True res += self.calc_patterns(used, i, length - 1) used[i] = False return res |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |