-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
484 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Solution(object): | ||
def isSubsequence(self, s, t): | ||
""" | ||
:type s: str | ||
:type t: str | ||
:rtype: bool | ||
""" | ||
i, j = 0, 0 | ||
while i < len(s) and j < len(t): | ||
if s[i] == t[j]: | ||
i += 1 | ||
j += 1 | ||
return i == len(s) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Solution(object): | ||
def totalHammingDistance(self, nums): | ||
""" | ||
:type nums: List[int] | ||
:rtype: int | ||
""" | ||
if not nums: | ||
return 0 | ||
res = 0 | ||
mask = 1 | ||
for i in range(32): | ||
cnt_one = 0 | ||
for num in nums: | ||
cnt_one += 1 if num & mask else 0 | ||
|
||
res += cnt_one * (len(nums) - cnt_one) | ||
mask = mask << 1 | ||
return res | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Definition for a binary tree node. | ||
# class TreeNode(object): | ||
# def __init__(self, x): | ||
# self.val = x | ||
# self.left = None | ||
# self.right = None | ||
|
||
class Solution(object): | ||
def diameterOfBinaryTree(self, root): | ||
""" | ||
:type root: TreeNode | ||
:rtype: int | ||
""" | ||
if not root: | ||
return 0 | ||
self.res = 0 | ||
def height(node): | ||
if not node: | ||
return 0 | ||
left_h = height(node.left) | ||
right_h = height(node.right) | ||
|
||
self.res = max(self.res, left_h + right_h) | ||
return 1 + max(left_h, right_h) | ||
height(root) | ||
return self.res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Solution(object): | ||
def findLongestChain(self, pairs): | ||
""" | ||
:type pairs: List[List[int]] | ||
:rtype: int | ||
""" | ||
pairs = sorted(pairs, key = lambda x: x[1]) | ||
|
||
end = pairs[0][0] - 1 | ||
res = 0 | ||
for pair in pairs: | ||
if pair[0] > end: | ||
res += 1 | ||
end = pair[1] | ||
|
||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class Solution(object): | ||
def numMatchingSubseq(self, S, words): | ||
""" | ||
:type S: str | ||
:type words: List[str] | ||
:rtype: int | ||
""" | ||
from collections import defaultdict | ||
|
||
dic = defaultdict(list) | ||
for i, ch in enumerate(S): | ||
dic[ch].append(i) | ||
|
||
res = 0 | ||
for word in words: | ||
pre = -1 | ||
flag = True | ||
for i, ch in enumerate(word): | ||
l = dic[ch] | ||
# 在l找第一个比pre大的元素 | ||
idx = bisect.bisect(l, pre) | ||
|
||
if idx == len(l):# 没找到 | ||
flag = False | ||
break | ||
pre = l[idx] | ||
|
||
if flag: | ||
res += 1 | ||
|
||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Solution(object): | ||
def countLetters(self, S): | ||
""" | ||
:type S: str | ||
:rtype: int | ||
""" | ||
res = 0 | ||
for i in range(len(S)): | ||
for j in range(i + 1, len(S) + 1): | ||
substring = S[i:j] | ||
if substring == substring[0] * len(substring): | ||
res += 1 | ||
# print substring | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class Solution(object): | ||
def uniqueOccurrences(self, arr): | ||
""" | ||
:type arr: List[int] | ||
:rtype: bool | ||
""" | ||
d = collections.Counter(arr) | ||
return len(d.values()) == len(set(d.values())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Solution(object): | ||
def arraysIntersection(self, arr1, arr2, arr3): | ||
""" | ||
:type arr1: List[int] | ||
:type arr2: List[int] | ||
:type arr3: List[int] | ||
:rtype: List[int] | ||
""" | ||
res = [] | ||
record = [0 for _ in range(2005)] | ||
for num in arr1 + arr2 + arr3: | ||
record[num] += 1 | ||
|
||
for i in range(len(record)): | ||
if record[i] == 3: | ||
res.append(i) | ||
|
||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution(object): | ||
def balancedStringSplit(self, s): | ||
""" | ||
:type s: str | ||
:rtype: int | ||
""" | ||
if not s: | ||
return 0 | ||
# print s | ||
l, r = 0, 0 | ||
|
||
for i in range(len(s)): | ||
if s[i] == "R": | ||
r += 1 | ||
else: | ||
l += 1 | ||
# print r, l | ||
if l == r: | ||
return 1 + self.balancedStringSplit(s[i + 1:]) | ||
|
||
return 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Solution(object): | ||
def checkStraightLine(self, coordinates): | ||
""" | ||
:type coordinates: List[List[int]] | ||
:rtype: bool | ||
""" | ||
c = sorted(coordinates, key = lambda x:x[0]) | ||
k = None | ||
for i in range(len(c)): | ||
if i: | ||
x0, y0 = c[i - 1][0], c[i - 1][1] | ||
x1, y1 = c[i][0], c[i][1] | ||
|
||
if x0 == x1: | ||
return False | ||
new_k = 1.0 * (y1 - y0) / (x1 - x0) | ||
if k and k != new_k: | ||
return False | ||
k = new_k | ||
|
||
return True | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Solution(object): | ||
def transformArray(self, arr): | ||
""" | ||
:type arr: List[int] | ||
:rtype: List[int] | ||
""" | ||
flag = 1 | ||
while flag: | ||
flag = 0 | ||
res = [num for num in arr] | ||
for i in range(1, len(arr) - 1): | ||
if arr[i - 1] < arr[i] and arr[i] > arr[i + 1]: | ||
res[i] -= 1 | ||
flag = 1 | ||
elif arr[i - 1] > arr[i] and arr[i] < arr[i + 1]: | ||
res[i] += 1 | ||
flag = 1 | ||
arr = res[:] | ||
return res | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from collections import defaultdict | ||
from heapq import * | ||
class Leaderboard(object): | ||
|
||
def __init__(self): | ||
self.dic = defaultdict(int) | ||
|
||
def addScore(self, playerId, score): | ||
""" | ||
:type playerId: int | ||
:type score: int | ||
:rtype: None | ||
""" | ||
self.dic[playerId] += score | ||
|
||
def top(self, K): | ||
""" | ||
:type K: int | ||
:rtype: int | ||
""" | ||
self.l = [] | ||
heapify(self.l) | ||
for pid, score in self.dic.items(): | ||
if len(self.l) >= K: | ||
if score > self.l[0]: | ||
heappush(self.l, score) | ||
heappop(self.l) | ||
else: | ||
heappush(self.l, score) | ||
|
||
return sum(self.l) | ||
|
||
|
||
def reset(self, playerId): | ||
""" | ||
:type playerId: int | ||
:rtype: None | ||
""" | ||
self.dic[playerId] = 0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from collections import defaultdict | ||
class Solution(object): | ||
def treeDiameter(self, edges): | ||
""" | ||
:type edges: List[List[int]] | ||
:rtype: int | ||
""" | ||
if not edges: | ||
return 0 | ||
|
||
self.neibors = defaultdict(set) | ||
self.res = 0 | ||
|
||
for start, end in edges: # 建树 | ||
self.neibors[start].add(end) | ||
|
||
def getHeight(node): | ||
res = [] | ||
for neibor in self.neibors[node]: | ||
res.append(getHeight(neibor)) | ||
|
||
while len(res) < 2: # 如果孩子少于两个,就给它补空的上去 | ||
res.append(0) | ||
|
||
res = sorted(res) | ||
self.res = max(self.res, sum(res[-2:])) # 取最长的两个子树长度 | ||
return 1 + max(res) | ||
|
||
getHeight(edges[0][0]) | ||
return self.res | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution(object): | ||
def minimumSwap(self, s1, s2): | ||
""" | ||
:type s1: str | ||
:type s2: str | ||
:rtype: int | ||
""" | ||
s = s1 + s2 | ||
x = s.count("x") | ||
if len(s1) != len(s2) or x % 2 == 1 or (len(s) - x) % 2 == 1: | ||
return -1 | ||
|
||
pair1 = 0 | ||
pair2 = 0 | ||
for i in range(len(s1)): | ||
if s1[i] == "y" and s2[i] == "x": | ||
pair1 += 1 | ||
elif s1[i] == "x" and s2[i] == "y": | ||
pair2 += 1 | ||
|
||
return pair1 // 2 + pair2 // 2 + pair1 % 2 + pair2 % 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class Solution(object): | ||
def numberOfSubarrays(self, nums, k): | ||
""" | ||
:type nums: List[int] | ||
:type k: int | ||
:rtype: int | ||
""" | ||
if not nums: | ||
return 0 | ||
res = 0 | ||
odd = [] | ||
for i, num in enumerate(nums): | ||
if num % 2: | ||
odd.append(i) | ||
|
||
if len(odd) < k: | ||
return 0 | ||
|
||
|
||
for i in range(len(odd)): | ||
if i + k > len(odd): | ||
break | ||
if i: | ||
last = odd[i - 1] | ||
else: | ||
last = -1 | ||
|
||
if i + k < len(odd): | ||
nxt = odd[i + k] | ||
else: | ||
nxt = len(nums) | ||
|
||
left = odd[i] - last | ||
right = nxt - odd[i + k - 1] | ||
res += left * right | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Solution(object): | ||
def minRemoveToMakeValid(self, s): | ||
""" | ||
:type s: str | ||
:rtype: str | ||
""" | ||
left, right = 0, 0 | ||
stack = [] | ||
remove = set() | ||
for i, ch in enumerate(s): | ||
if ch == "(": | ||
stack.append(i) | ||
elif ch == ")": | ||
if stack: | ||
stack.pop() | ||
else: | ||
remove.add(i) | ||
stack = set(stack) | ||
res = "" | ||
for i, ch in enumerate(s): | ||
if i not in stack and i not in remove: | ||
res += ch | ||
return res |
Oops, something went wrong.