Given two stings ransomNote and magazine, return true if ransomNote can be constructed from magazine and false otherwise.
Each letter in magazine can only be used once in ransomNote.
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
ran = collections.Counter(ransomNote)
mag = collections.Counter(magazine)
for k, v in ran.items():
if k not in mag or mag[k] < v:
return False
return True
Counter 써서 ran
에 있는 모든 값들이 mag
에 포함되는지 확인해줌
You are given a nested list of integers nestedList. Each element is either an integer or a list whose elements may also be integers or other lists. Implement an iterator to flatten it.
Implement the NestedIterator class:
List<NestedInteger> nestedList
) Initializes the iterator with the nested list nestedList.Your code will be tested with the following pseudocode:
initialize iterator with nestedList
res = []
while iterator.hasNext()
append iterator.next() to the end of res
return res
If res matches the expected flattened list, then your code will be judged as correct.
# """
# This is the interface that allows for creating nested lists.
# You should not implement it, or speculate about its implementation
# """
#class NestedInteger:
# def isInteger(self) -> bool:
# """
# @return True if this NestedInteger holds a single integer, rather than a nested list.
# """
#
# def getInteger(self) -> int:
# """
# @return the single integer that this NestedInteger holds, if it holds a single integer
# Return None if this NestedInteger holds a nested list
# """
#
# def getList(self) -> [NestedInteger]:
# """
# @return the nested list that this NestedInteger holds, if it holds a nested list
# Return None if this NestedInteger holds a single integer
# """
class NestedIterator:
def __init__(self, nestedList: [NestedInteger]):
self.nested = nestedList
self.list = []
self.i = 0
for i in range(len(self.nested)):
if self.nested[i].isInteger() == 0:
tmp = self.nested[i].getList()
for j in range(len(tmp)):
self.list.append(tmp[j].getInteger())
else:
self.list.append(self.nested[i].getInteger())
def next(self) -> int:
self.i += 1
return self.list[self.i-1]
def hasNext(self) -> bool:
if self.i < len(self.list):
return 1
return 0
# Your NestedIterator object will be instantiated and called as such:
# i, v = NestedIterator(nestedList), []
# while i.hasNext(): v.append(i.next())
넘 그지같아서 아예 init 에서 self.list
에 값들을 다 저장하고
리스트 값들을 return 하는 거로 하려했으나..
모든 값들을 가져오는 거부터...^^ 넘 헷갈려서 잘 안되네요
# """
# This is the interface that allows for creating nested lists.
# You should not implement it, or speculate about its implementation
# """
#class NestedInteger:
# def isInteger(self) -> bool:
# """
# @return True if this NestedInteger holds a single integer, rather than a nested list.
# """
#
# def getInteger(self) -> int:
# """
# @return the single integer that this NestedInteger holds, if it holds a single integer
# Return None if this NestedInteger holds a nested list
# """
#
# def getList(self) -> [NestedInteger]:
# """
# @return the nested list that this NestedInteger holds, if it holds a nested list
# Return None if this NestedInteger holds a single integer
# """
class NestedIterator:
def __init__(self, nestedList: [NestedInteger]):
self.queue = []
self.flattenList(nestedList)
def next(self) -> int:
return self.queue.pop(0)
def hasNext(self) -> bool:
return len(self.queue) > 0
def flattenList(self, nestedList):
for item in nestedList:
if(not item.isInteger()):
self.flattenList(item.getList())
else:
self.queue.append(item.getInteger())
# Your NestedIterator object will be instantiated and called as such:
# i, v = NestedIterator(nestedList), []
# while i.hasNext(): v.append(i.next())
이것도 init 에서 아예 모든 값들을 queue
에 저장
저장할 때 flattenList
함수를 이용해서 integer 가 나올 때까지 리스트를 재귀 돌려줌