-
Notifications
You must be signed in to change notification settings - Fork 43
/
prime-palindrome.py
87 lines (74 loc) · 2.44 KB
/
prime-palindrome.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# V1 : dev
# class Solution(object):
# def primePalindrome(self, N):
# def isPalindrome(self, x):
# if x < 0:
# return False
# x = str(x)
# # if x is like 12321 (odd length)
# if str(x)[:math.floor(len(str(x))/2)] == x[math.floor(len(str(x))/2)+1:][::-1]:
# return True
# # if x is like 2222 (even length)
# if str(x)[:math.floor(len(str(x))/2)] == x[math.floor(len(str(x))/2):][::-1]:
# return True
# else:
# return False
# def is_prime(n):
# if n < 2 or n % 2 == 0:
# return n == 2
# return all(n % d for d in xrange(3, int(n**.5) + 1, 2))
# if 8 <= N <= 11:
# return 11
# for i in xrange(10**(len(str(N))//2), 10**5):
# j = int(str(i) + str(i)[-2::-1])
# if j >= N and is_prime(j):
# return j
# V2
# https://www.kancloud.cn/kancloud/data-structure-and-algorithm-notes/73059
### DFS algorithm ###
class Solution:
# @param s, a string
# @return a list of lists of string
def partition(self, s):
result = []
if not s:
return result
palindromes = []
self.dfs(s, 0, palindromes, result)
return result
def dfs(self, s, pos, palindromes, ret):
if pos == len(s):
ret.append([] + palindromes)
return
for i in range(pos + 1, len(s) + 1):
if not self.isPalindrome(s[pos:i]):
continue
palindromes.append(s[pos:i])
self.dfs(s, i, palindromes, ret)
palindromes.pop()
def isPalindrome(self, s):
if not s:
return False
# reverse compare
return s == s[::-1]
# V3
# Time: O(n^(1/2) * (logn + n^(1/2)))
# Space: O(logn)
class Solution(object):
def primePalindrome(self, N):
"""
:type N: int
:rtype: int
"""
def is_prime(n):
if n < 2 or n % 2 == 0:
return n == 2
# all() func in python
# https://www.jianshu.com/p/65b6b4a62071
return all(n % d for d in range(3, int(n**.5) + 1, 2))
if 8 <= N <= 11:
return 11
for i in range(10**(len(str(N))//2), 10**5):
j = int(str(i) + str(i)[-2::-1])
if j >= N and is_prime(j):
return j