Skip to content

Commit c5255bf

Browse files
committed
完美平方
1 parent fd60fdd commit c5255bf

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

perfect_squares.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
3+
class Solution:
4+
# @param {int} n a positive integer
5+
# @return {int} an integer
6+
def numSquares(self, n):
7+
# Write your code here
8+
# Python效率问题,代码翻译成Java可通过。
9+
cache = [2147483647] * (n + 1)
10+
cache[0] = 0
11+
for i in xrange(n + 1):
12+
for j in xrange(1, n + 1):
13+
if (i + j * j) <= n:
14+
cache[i + j * j] = min(cache[i + j * j], cache[i] + 1)
15+
else:
16+
break
17+
return cache[-1]

0 commit comments

Comments
 (0)