class Solution:
def findMissingRanges(self, nums, lower: int, upper: int):
nums.insert(0, lower-1) # Left Bound
nums.append(upper+1) # Right Bound
res = []
for i in range(len(nums)-1):
left, right = nums[i], nums[i + 1]
if left != right:
if right - left == 2:
res.append(str(left+1))
elif right - left > 2:
res.append(str(left+1) + "->" + str(right-1))
return res
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.
Your algorithm should run in O(n) complexity.
Example:
Input: [100, 4, 200, 1, 3, 2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
nums = set(nums)
ans = 0
for x in nums:
# Find the first element in one segment
if x-1 not in nums:
y = x + 1
# reach the last consecutive element in one element
while y in nums:
y += 1
ans = max(ans, y - x)
return ans
class Solution:
def longestConsecutive(self, nums) -> int:
UF = {}
nums = set(nums)
if not nums:
return 0
# for num in nums:
# UF[num] = num
def find(x):
if x != UF[x]:
UF[x] = find(UF[x])
return UF[x]
def union(x, y):
UF.setdefault(x, x)
UF.setdefault(y, y)
UF[find(x)] = find(y)
for n in nums:
if (n - 1) in nums:
union(n - 1, n)
if (n + 1) in nums:
union(n, n + 1)
ans = 1
for num in nums:
if num in UF:
ans = max(ans, find(num) - num + 1)
return ans
# coding=utf8
import os
import sys
import atexit
def daemonize(pid_file=None):
"""
Create daemon process
:param pid_file: File that saves process id
:return:
"""
# Fork a subprocess from parent process
pid = os.fork()
# The id of subprocess equals 0, and of parent process must larger than 0
if pid:
# Exit from parent process(sys.exit() executes flush while os._exit() doesn't)
sys.exit(0)
# The subprocess defaults the working directory of its parent process.
os.chdir('/')
# The subprocess defaults umask(File permission mask), reset it to 0(Fully control) in order to avoiding R/W operations.
os.umask(0)
# Let the subprocess be the leader of the conversation and the process.
os.setsid()
# Second Fork
_pid = os.fork()
if _pid:
# Exit subprocess
sys.exit(0)
# Grandson process is daemon right now. Redirecting stdin/stdout/stderr descriptions to avoid errors in the printing process.
# Flush stdout/stderr buffer
sys.stdout.flush()
sys.stderr.flush()
# Atomically close and duclipate file description, and redirect into /dev/null(Drop off all input and output)
with open('/dev/null') as read_null, open('/dev/null', 'w') as write_null:
os.dup2(read_null.fileno(), sys.stdin.fileno())
os.dup2(write_null.fileno(), sys.stdout.fileno())
os.dup2(write_null.fileno(), sys.stderr.fileno())
# Write PID
if pid_file:
with open(pid_file, 'w+') as f:
f.write(str(os.getpid()))
# Register Exit function for abnormal exit
atexit.register(os.remove, pid_file)
Fork sub-process, and exit parent-process.
Change Working Directory (chdir), File Permission Mask(umask), Process Group and Conversation Group (setsid).
Fork grandson-process, and exit sub-process.
Flush buffer and atomically close and duclipate file description, and redirect into /dev/null(Drop off all input and output)
At very first I read this problem, the MOVE operation is described as confusing as heck. I read some explanations on the discussboard, and then finally got the actual meaning.
We can treat each coordinate as two parts (x and y), and can maintain an Union-Find in order to calculate how many groups in the entire field.
class Solution:
def removeStones(self, stones: List[List[int]]) -> int:
UF = {}
def find(x):
if x != UF[x]:
UF[x] = find(UF[x])
return UF[x]
def union(x, y):
UF.setdefault(x, x)
UF.setdefault(y, y)
UF[find(x)] = find(y)
for i, j in stones:
print(i, j, ~j)
union(i, ~j)
return len(stones) - len({find(x) for x in UF})
Each letter in the word has 1 or more options. If there is one option, the letter is represented as is. If there is more than one option, then curly braces delimit the options. For example, "{a,b,c}" represents options ["a", "b", "c"].
For example, "{a,b,c}d{e,f}" represents the list ["ade", "adf", "bde", "bdf", "cde", "cdf"].
Return all words that can be formed in this manner, in lexicographical order.
All characters inside a pair of consecutive opening and ending curly brackets are different.
class Solution:
def expand(self, S):
def func(remain, result, results):
if not remain:
results.append(result)
else:
# if a single character appear at the first of the remain string
if remain[0] != "{":
func(remain[1:], result + remain[0], results)
return
else:
temp = list()
i = 0
# find elements in the first brace of the remain string
while remain[i] != "}":
if remain[i].isalpha():
temp.append(remain[i])
i += 1
for t in temp:
func(remain[i + 1:], result + t, results)
results = []
func(S, "", results)
return results
I wanted you to see what real courage is, instead of getting the idea that courage is a man with a gun in his hand. It’s when you know you’re licked before you begin anyway and you see it through no matter what. You rarely win, but sometimes you do.
Given an encoded string, return its decoded string.
The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.
You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.
Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won’t be input like 3a or 2[4].
Examples:
s = "3[a]2[bc]", return "aaabcbc".
s = "3[a2]", return "accaccacc".
s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
class Solution:
def decodeString(self, s: str) -> str:
stack = list()
times = list()
for i, v in enumerate(s):
if v.isdigit():
# Single Digit, like 1,2,3...
if not s[i-1].isdigit():
times.append(int(v))
# Multiple digits, like 100, 200...
else:
times[-1] = times[-1] * 10 + int(v)
# push
elif v != "]":
stack.append(v)
# reach "]"
else:
# Retrive string in cloest level square brackets
b = ""
t = stack.pop()
while t != "[":
b = t + b
t = stack.pop()
stack.append(b * times.pop())
return "".join(stack)