擅长:python、mysql、java
<p>试试这个,不会花31秒的时间:</p>
<pre><code>def isPalindrome(n):
return str(n) == str(n)[::-1]
def listNums():
a, b, pal = 0, 0, 0;
for i in range(999, 99, -1):
for j in range(999, 99, -1):
n = i * j
if isPalindrome(n) and n > pal: # better to use "n > pal and isPalindrome(n)" instead, see other answer for details.
a, b, pal = i, j, n
return a, b, pal
print listNums()
</code></pre>
<p>运行此操作大约需要1秒。对于这样的东西,你当然不需要在循环中使用那些多余的<code>if</code>语句——如果你循环,比如说,<code>range(9999, 999, -1)</code>,你可以考虑做一些类似的优化(当然,有很多潜在的优化可以对类似的东西进行,例如,不要在每个i,j对中循环两次)。在</p>