Skip to content

Commit 03ecc5e

Browse files
authored
Create count-symmetric-integers.py
1 parent e39f145 commit 03ecc5e

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Python/count-symmetric-integers.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Time: O(rlogr)
2+
# Space: O(r)
3+
4+
# brute force, memoization
5+
MAX_R = 10**4
6+
LOOKUP = [-1]*MAX_R
7+
class Solution(object):
8+
def countSymmetricIntegers(self, low, high):
9+
"""
10+
:type low: int
11+
:type high: int
12+
:rtype: int
13+
"""
14+
def check(x):
15+
if LOOKUP[x-1] == -1:
16+
digits = map(int, str(x))
17+
if len(digits)%2:
18+
LOOKUP[x-1] = 0
19+
else:
20+
LOOKUP[x-1] = int(sum(digits[i] for i in xrange(len(digits)//2)) == sum(digits[i] for i in xrange(len(digits)//2, len(digits))))
21+
return LOOKUP[x-1]
22+
23+
return sum(check(x) for x in xrange(low, high+1))

0 commit comments

Comments
 (0)