diff --git a/contains-duplicate/chapse57.py b/contains-duplicate/chapse57.py new file mode 100644 index 0000000000..1cb32db06f --- /dev/null +++ b/contains-duplicate/chapse57.py @@ -0,0 +1,11 @@ +class Solution(object): + def containsDuplicate(self, nums): + """ + :type nums: List[int] + :rtype: bool + """ + for i in range(len(nums)): + for j in range(i+1,len(nums)): + if nums[i] == nums[j]: + return True + return False diff --git a/top-k-frequent-elements/chapse57.py b/top-k-frequent-elements/chapse57.py new file mode 100644 index 0000000000..99cd374eb8 --- /dev/null +++ b/top-k-frequent-elements/chapse57.py @@ -0,0 +1,22 @@ +class Solution(object): + def topKFrequent(self, nums, k): + """ + :type nums: List[int] + :type k: int + :rtype: List[int] + """ + count = {} + for n in nums: + if n in count: + count[n] +=1 + else: + count[n] =1 + # count.items()를 횟수 큰 순으로 정렬 + freq = sorted(count.items(), key=lambda x: x[1], reverse=True) + + result = [] + for x in freq[:k]: + result.append(x[0]) + return result + + diff --git a/two-sum/chapse57.py b/two-sum/chapse57.py new file mode 100644 index 0000000000..b83b732ef1 --- /dev/null +++ b/two-sum/chapse57.py @@ -0,0 +1,14 @@ +class Solution(object): + def twoSum(self, nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: List[int] + """ + seen = {} + for i in range(len(nums)): + if (target - nums[i]) in seen: + return [seen[target - nums[i]],i] + seen[nums[i]] =i + +