-
-
Save licaomeng/694df2aefd73b2fc651a851ab629b21b to your computer and use it in GitHub Desktop.
indeed2
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
def summmaryranges(nums): | |
ranges = [] | |
for num in nums: | |
if not ranges or num - ranges[-1][-1] > 1: | |
ranges.append([]) | |
ranges[-1][1:] = [num] | |
return ['->'.join(map(str, r)) for r in ranges] | |
def reverse(l, r, cl): | |
while l < r: | |
cl[l], cl[r] = cl[r], cl[l] | |
l += 1 | |
r -= 1 | |
def reversestring(s): | |
n = len(s) | |
s = list(s) | |
reverse(0, n - 1, s) | |
l = 0 | |
for r in range(1, n + 1): | |
if r == n or s[r] == ' ': | |
reverse(l, r - 1, s) | |
l = r + 1 | |
return ''.join(s) | |
def reverseHTML(h): | |
n = len(h) | |
h = list(h[::-1]) | |
l = 0 | |
while l < n - 1: | |
if l == ';': | |
while l < n and h[l] == h[l - 1]: | |
l += 1 | |
r = l + 1 | |
while r < n and h[r] != '&': | |
r += 1 | |
reverse(l, r, h) | |
l = r + 1 | |
l += 1 | |
return ''.join(h) | |
def findduplicate(s): | |
d = {} | |
for word in s.split(): | |
d[word] = d.get(word, 0) + 1 | |
return [item[0] for item in filter(lambda x:x[1] > 1, d.items())] | |
def findfirstdup(s): | |
d = {} | |
words = s.split() | |
min_idx = len(words) | |
for idx, word in enumerate(words): | |
if word not in d: | |
d[word] = idx | |
else: | |
min_idx = min(min_idx, d[word]) | |
if min_idx == len(words): | |
return None | |
return words[min_idx] | |
def a2num(s): | |
''' | |
A 1 AA 26 + 1 | |
B 2 AB 26 + 2 | |
Z 26 AZ 26 + 26 | |
''' | |
res = 0 | |
for idx, c in enumerate(s[::-1]): | |
res += (ord(c) - ord('A') + 1) * 26 ** idx | |
return res | |
def num2a(n): | |
res = '' | |
while n > 0: | |
res += chr((n - 1) % 26 + ord('A')) | |
n = (n - 1) // 26 | |
return res[::-1] | |
def findkbucket(nums, k): | |
d = {} | |
res = [] | |
for num in nums: | |
d[num] = d.get(num, 0) + 1 | |
freqdict = [[] for _ in range(len(nums) + 1)] | |
for key in d: | |
freqdict[d[key]] += [key] | |
for item in freqdict[::-1]: | |
res += item | |
return res[:k] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment