Skip to content

Commit 7839532

Browse files
committed
Sync LeetCode submission Runtime - 95 ms (97.56%), Memory - 17.8 MB (81.26%)
1 parent 36af37e commit 7839532

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<p>You are given two positive integers <code>low</code> and <code>high</code>.</p>
2+
3+
<p>An integer <code>x</code> consisting of <code>2 * n</code> digits is <strong>symmetric</strong> if the sum of the first <code>n</code> digits of <code>x</code> is equal to the sum of the last <code>n</code> digits of <code>x</code>. Numbers with an odd number of digits are never symmetric.</p>
4+
5+
<p>Return <em>the <strong>number of symmetric</strong> integers in the range</em> <code>[low, high]</code>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> low = 1, high = 100
12+
<strong>Output:</strong> 9
13+
<strong>Explanation:</strong> There are 9 symmetric integers between 1 and 100: 11, 22, 33, 44, 55, 66, 77, 88, and 99.
14+
</pre>
15+
16+
<p><strong class="example">Example 2:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> low = 1200, high = 1230
20+
<strong>Output:</strong> 4
21+
<strong>Explanation:</strong> There are 4 symmetric integers between 1200 and 1230: 1203, 1212, 1221, and 1230.
22+
</pre>
23+
24+
<p>&nbsp;</p>
25+
<p><strong>Constraints:</strong></p>
26+
27+
<ul>
28+
<li><code>1 &lt;= low &lt;= high &lt;= 10<sup>4</sup></code></li>
29+
</ul>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Approach 1: Enumeration
2+
3+
# Time: O(high - low)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def countSymmetricIntegers(self, low: int, high: int) -> int:
8+
res = 0
9+
10+
for x in range(low, high + 1):
11+
if x < 100 and x % 11 == 0:
12+
res += 1
13+
if 1000 <= x < 10000:
14+
left = x // 1000 + x % 1000 // 100
15+
right = x % 100 // 10 + x % 10
16+
if left == right:
17+
res += 1
18+
return res
19+

0 commit comments

Comments
 (0)