Skip to content

Commit de02576

Browse files
author
Cenkay
committed
Initial commit
0 parents  commit de02576

File tree

1,165 files changed

+17817
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,165 files changed

+17817
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Cenkay Arapisaoglu
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 1196 additions & 0 deletions
Large diffs are not rendered by default.

solutions/python/160.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution(object):
2+
def getIntersectionNode(self, headA, headB):
3+
r1, r2, l1, l2 = headA, headB, 0, 0
4+
while headA: headA, l1 = headA.next, l1 + 1
5+
while headB: headB, l2 = headB.next, l2 + 1
6+
while l1 > l2: r1, l1 = r1.next, l1 - 1
7+
while l2 > l1: r2, l2 = r2.next, l2 - 1
8+
while r1 != r2: r1, r2 = r1.next, r2.next
9+
return r1

solutions/python/190.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
# @param n, an integer
3+
# @return an integer
4+
def reverseBits(self, n):
5+
return int(bin(n)[2:].zfill(32)[::-1],2)

solutions/python/271.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Codec:
2+
3+
def encode(self, strs):
4+
return "".join(str(len(s)) + ":" + s for s in strs)
5+
6+
def decode(self, s):
7+
strs = []
8+
while s:
9+
i = s.find(":")
10+
length = int(s[:i])
11+
s = s[i+1:]
12+
strs.append(s[:length])
13+
s = s[length:]
14+
return strs

solutions/python/277.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# The knows API is already defined for you.
2+
# @param a, person a
3+
# @param b, person b
4+
# @return a boolean, whether a knows b
5+
# def knows(a, b):
6+
7+
class Solution(object):
8+
def findCelebrity(self, n):
9+
i = 0
10+
while i < n:
11+
person = i
12+
i += 1
13+
while i < n and not knows(person, i) and knows(i, person):
14+
i += 1
15+
j = person - 1
16+
while j >= 0 and not knows(person, j) and knows(j, person):
17+
j -= 1
18+
return person if j < 0 else -1

solutions/python/281.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class ZigzagIterator(object):
2+
3+
def __init__(self, v1, v2):
4+
self.arr1, self.arr2, self.l1, self.l2 = v1, v2, len(v1), len(v2)
5+
self.i = self.j = 0
6+
self.n, self.turn = max(self.l1, self.l2), v1 and 1 or 0
7+
8+
def next(self):
9+
if self.turn:
10+
num = self.arr1[self.i]
11+
self.i += 1
12+
if self.j < self.l2:
13+
self.turn = 0
14+
else:
15+
num = self.arr2[self.j]
16+
self.j += 1
17+
if self.i < self.l1:
18+
self.turn = 1
19+
return num
20+
21+
def hasNext(self):
22+
return self.turn and self.i < self.l1 or self.j < self.l2

solutions/python/374.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution(object):
2+
def guessNumber(self, n):
3+
l, r, g = 1, n, 1
4+
while g and l <= r:
5+
m = (l + r) // 2
6+
g = guess(m)
7+
if g == 1:
8+
l = m + 1
9+
else:
10+
r = m
11+
return m

solutions/python/400.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution(object):
2+
def findNthDigit(self, n):
3+
start, size, step = 1, 1, 9
4+
while n > size * step:
5+
n, size, step, start = n - (size * step), size + 1, step * 10, start * 10
6+
return int(str(start + (n - 1) // size)[(n - 1) % size])

solutions/python/423.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution(object):
2+
def originalDigits(self, s):
3+
res = ""
4+
res += "0"*s.count('z')
5+
res += "1"*(s.count('o')-s.count('z')-s.count('w')-s.count('u'))
6+
res += "2"*s.count('w')
7+
res += "3"*(s.count('h') - s.count('g'))
8+
res += "4"*s.count('u')
9+
res += "5"*(s.count('f') - s.count('u'))
10+
res += "6"*s.count('x')
11+
res += "7"*(s.count('s')-s.count('x'))
12+
res += "8"*s.count("g")
13+
res += "9"*(s.count('i') - s.count('x') - s.count("g") - s.count('f') + s.count('u'))
14+
return res

solutions/python/428.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Codec:
2+
3+
def serialize(self, root):
4+
arr = []
5+
def dfs(node):
6+
if not node: return
7+
arr.append(str(node.val))
8+
for child in node.children:
9+
dfs(child)
10+
arr.append("#")
11+
dfs(root)
12+
return " ".join(arr)
13+
14+
def deserialize(self, data):
15+
if not data: return None
16+
data = collections.deque(data.split(" "))
17+
root = Node(int(data.popleft()), [])
18+
q = [root]
19+
while data:
20+
val = data.popleft()
21+
if val == "#":
22+
q.pop()
23+
else:
24+
new = Node(int(val), [])
25+
q[-1].children.append(new)
26+
q.append(new)
27+
return root

solutions/python/429.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution(object):
2+
def levelOrder(self, root):
3+
q, ret = [root], []
4+
while any(q):
5+
ret.append([node.val for node in q])
6+
q = [child for node in q for child in node.children if child]
7+
return ret

solutions/python/430.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def flatten(self, head):
3+
stack, cur, root = [], head, head
4+
while stack or cur:
5+
if cur.child:
6+
if cur.next:
7+
stack.append(cur.next)
8+
new = cur.child
9+
new.prev = cur
10+
cur.child = None
11+
cur.next = cur = new
12+
elif not cur.next and stack:
13+
cur.next = stack.pop()
14+
cur.next.prev = cur
15+
cur = cur.next
16+
else:
17+
cur = cur.next
18+
return root

solutions/python/431.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""
2+
# Definition for a Node.
3+
class Node(object):
4+
def __init__(self, val, children):
5+
self.val = val
6+
self.children = children
7+
"""
8+
"""
9+
# Definition for a binary tree node.
10+
class TreeNode(object):
11+
def __init__(self, x):
12+
self.val = x
13+
self.left = None
14+
self.right = None
15+
"""
16+
class Codec:
17+
18+
def encode(self, root):
19+
"""Encodes an n-ary tree to a binary tree.
20+
21+
:type root: Node
22+
:rtype: TreeNode
23+
"""
24+
if not root:
25+
return None
26+
27+
binary = TreeNode(root.val) # create a binary root
28+
if not root.children:
29+
return binary
30+
31+
binary.left = self.encode(root.children[0]) # left child of binary is the encoding of all n-ary children,
32+
node = binary.left # starting with the first child.
33+
for child in root.children[1:]: # other children of n-ary root are right child of previous child
34+
node.right = self.encode(child)
35+
node = node.right
36+
37+
return binary
38+
39+
def decode(self, data):
40+
"""Decodes your binary tree to an n-ary tree.
41+
42+
:type data: TreeNode
43+
:rtype: Node
44+
"""
45+
if not data:
46+
return None
47+
48+
nary = Node(data.val, []) # create n-ary root
49+
node = data.left # move to first child of n-ary root
50+
while node: # while more children of n-ary root
51+
nary.children.append(self.decode(node)) # append to list
52+
node = node.right # and move to next child
53+
54+
return nary
55+
56+
# Your Codec object will be instantiated and called as such:
57+
# codec = Codec()
58+
# codec.decode(codec.encode(root))

solutions/python/440.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def findKthNumber(self, n, k):
3+
def calSteps(n1, n2):
4+
steps = 0
5+
while n1 <= n:
6+
steps += min(n + 1, n2) - n1
7+
n1 *= 10
8+
n2 *= 10
9+
return steps
10+
cur = 1
11+
k -= 1
12+
while k:
13+
steps = calSteps(cur, cur + 1)
14+
if steps <= k:
15+
cur += 1
16+
k -= steps
17+
else:
18+
cur *= 10
19+
k -= 1
20+
return cur

solutions/python/447.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution(object):
2+
def numberOfBoomerangs(self, points):
3+
res = 0
4+
for p in points:
5+
dic = collections.defaultdict(int)
6+
for q in points:
7+
d = (p[0] - q[0]) ** 2 + (p[1] - q[1]) ** 2
8+
dic[d] += 1
9+
for k in dic: res += dic[k] * (dic[k] - 1)
10+
return res

solutions/python/449.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Codec:
9+
10+
def serialize(self, root):
11+
"""Encodes a tree to a single string.
12+
13+
:type root: TreeNode
14+
:rtype: str
15+
"""
16+
return root
17+
18+
19+
def deserialize(self, data):
20+
"""Decodes your encoded data to tree.
21+
22+
:type data: str
23+
:rtype: TreeNode
24+
"""
25+
return data
26+
27+
28+
# Your Codec object will be instantiated and called as such:
29+
# codec = Codec()
30+
# codec.deserialize(codec.serialize(root))

solutions/python/457.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution(object):
2+
def circularArrayLoop(self, nums):
3+
loops, n = [[set(), set()] for _ in range(len(nums))], len(nums)
4+
for loop in range(3):
5+
for i, num in enumerate(nums):
6+
mode = 0 if num > 0 else 1
7+
if loop == 2 and i in loops[i][mode] and i != (i + num) % n: return True
8+
loops[(i + num) % n][mode] |= loops[i][mode] or {i}
9+
return False

solutions/python/458.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution(object):
2+
def poorPigs(self, buckets, minutesToDie, minutesToTest):
3+
"""
4+
:type buckets: int
5+
:type minutesToDie: int
6+
:type minutesToTest: int
7+
:rtype: int
8+
"""
9+
pigs = 0
10+
while (minutesToTest / minutesToDie + 1) ** pigs < buckets: pigs += 1
11+
return pigs

solutions/python/518.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution(object):
2+
def change(self, amount, coins):
3+
dp = [1] + [0] * amount
4+
for coin in coins:
5+
for i in range(coin, amount + 1):
6+
dp[i] += dp[i - coin]
7+
return dp[-1]

solutions/python/535.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Codec:
2+
3+
def encode(self, longUrl): return longUrl
4+
def decode(self, shortUrl): return shortUrl
5+
# Your Codec object will be instantiated and called as such:
6+
# codec = Codec()
7+
# codec.decode(codec.encode(url))

solutions/python/548.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def splitArray(self, nums):
3+
n = len(nums)
4+
s = [0] * (n + 1)
5+
for i in range(n): s[i + 1] = s[i] + nums[i]
6+
def check(l, r):
7+
return set(s[m] - s[l] for m in range(l + 1, r + 1) if s[m] - s[l] == s[r + 1] - s[m + 1])
8+
return any(check(0, j - 1) & check(j + 1, n - 1) for j in range(n))

solutions/python/552.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def checkRecord(self, n):
3+
dp, mod = [1, 0, 0, 1, 1, 0], 10 ** 9 + 7
4+
for i in range(1, n):
5+
dp[0], dp[1], dp[2], dp[3], dp[4], dp[5] = sum(dp) % mod, dp[0], dp[1], sum(dp[3:]) % mod, dp[3], dp[4]
6+
return sum(dp) % mod

solutions/python/559.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution(object):
2+
def maxDepth(self, root, level = 1):
3+
return max(root and [self.maxDepth(child, level + 1) for child in root.children] + [level] or [0])

solutions/python/589.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution(object):
2+
def preorder(self, root):
3+
ret, q = [], collections.deque([root])
4+
while any(q):
5+
node = q.popleft()
6+
ret.append(node.val)
7+
for child in node.children[::-1]:
8+
if child: q.appendleft(child)
9+
return ret

0 commit comments

Comments
 (0)