Skip to content

Commit 74a3446

Browse files
author
wuduhren
committed
update
1 parent 733fd02 commit 74a3446

File tree

5 files changed

+91
-0
lines changed

5 files changed

+91
-0
lines changed

problems/python3/detect-squares.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class DetectSquares:
2+
3+
def __init__(self):
4+
self.store = collections.Counter()
5+
6+
def add(self, point: List[int]) -> None:
7+
self.store[tuple(point)] += 1
8+
9+
def count(self, point: List[int]) -> int:
10+
x, y = point
11+
ans = 0
12+
13+
for dx, dy in self.store:
14+
if abs(x-dx)!=abs(y-dy) or x==dx or y==dy: continue
15+
ans += self.store[(dx, dy)]*self.store[(dx, y)]*self.store[(x, dy)]
16+
return ans
17+
18+
19+
# Your DetectSquares object will be instantiated and called as such:
20+
# obj = DetectSquares()
21+
# obj.add(point)
22+
# param_2 = obj.count(point)

problems/python3/happy-number.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def isHappy(self, n: int) -> bool:
3+
def digitSquare(n) -> int:
4+
ans = 0
5+
while n>0:
6+
ans += (n%10)**2
7+
n = n//10
8+
return ans
9+
10+
visited = set()
11+
visited.add(1)
12+
13+
while n not in visited:
14+
visited.add(n)
15+
n = digitSquare(n)
16+
17+
return n==1

problems/python3/multiply-strings.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def multiply(self, num1: str, num2: str) -> str:
3+
if num1=='0' or num2=='0': return '0'
4+
M, N = len(num1), len(num2)
5+
temp = [0]*(M+N+1)
6+
7+
num1, num2 = num1[::-1], num2[::-1]
8+
for i in range(M):
9+
for j in range(N):
10+
digits = int(num1[i])*int(num2[j])
11+
temp[i+j] += digits
12+
temp[i+j+1] += temp[i+j]//10
13+
temp[i+j] = temp[i+j]%10
14+
15+
ans = ''
16+
temp = temp[::-1]
17+
isLeadingZero = True
18+
for d in temp:
19+
if d!=0 or not isLeadingZero:
20+
isLeadingZero = False
21+
ans += str(d)
22+
return ans

problems/python3/plus-one.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def plusOne(self, digits: List[int]) -> List[int]:
3+
i = len(digits)-1
4+
needAdditionDigit = True
5+
6+
while i>=0 and needAdditionDigit:
7+
if digits[i]==9:
8+
digits[i] = 0
9+
i -= 1
10+
needAdditionDigit = True
11+
else:
12+
digits[i] += 1
13+
needAdditionDigit = False
14+
if needAdditionDigit: digits.insert(0, 1)
15+
return digits
16+

problems/python3/powx-n.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def myPow(self, x: float, k: int) -> float:
3+
if k<0: return 1/self.myPow(x, -k)
4+
5+
if k==0:
6+
return 1
7+
elif k==1:
8+
return x
9+
elif k%2==0:
10+
half = self.myPow(x, k//2)
11+
return half * half
12+
else:
13+
half = self.myPow(x, (k-1)//2)
14+
return half * half * x

0 commit comments

Comments
 (0)