You are given an array of logs. Each log is a space-delimited string of words, where the first word is the identifier.
There are two types of logs:
Return the final order of the logs.
class Solution:
def reorderLogFiles(self, logs: List[str]) -> List[str]:
# 이거 easy 맞나요...?
result = [] # 식별자 제외한 문자 값들만 저장
nums = [] # 식별자 포함한 숫자 값들만 저장
dic = {} # [식별자: logs 값]
for i in range(len(logs)):
tmp = logs[i].split()
# 문자로 구성됐으면 dic, result 에 넣어줌
if tmp[1].isalpha() and tmp[0] not in dic:
dic[tmp[0]] = [" ".join(tmp[1:])]
result.append(" ".join(tmp[1:]))
# 식별자가 중복되는 경우... 원래 있는 dic 값에 플러스
elif tmp[1].isalpha() and tmp[0] in dic:
dic[tmp[0]] += [" ".join(tmp[1:])]
result.append(" ".join(tmp[1:]))
# 숫자로 구성된 건 nums 에 넣어줌
else:
nums.append(logs[i])
# 문자들 기준으로 sort
result.sort()
# 식별자 기준으로 sort
sdic = sorted(dic.items())
for key, val in sdic:
for v in val: # 식별자가 중복된 경우 여러개의 값이므로...
for i in range(len(result)):
# result 값들의 key 찾아주기
if v == result[i]:
result[i] = key + " " + result[i]
break
# 숫자는 정렬 필요없으니까 뒤에 붙여주기
result += nums
return result
정말.. 구질구질하게 풀었읍니다
애플아 너 왜 이딴 문제 내... 우리 좋았잖아...^^
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
def func(o, c, s):
if c == n:
self.result.add(s)
return s
if o == c or o < n:
func(o+1, c, s + "(")
if o > c and c < n:
func(o, c+1, s + ")")
return s
self.result = set()
func(0, 0, "")
return self.result
o(pen)
c(lose)
써서 조합 찾기
재귀는 아직도 어렵네요...ㅎ