Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 |
Tags
- 백준
- 파이썬
- k8s
- GCP
- java
- Codility
- docker
- cpu scheduling
- go
- Dynamic Programming
- 알고리즘
- Backjoon
- 피보나치
- Observer Pattern
- 그리디
- KAKAO
- Python
- Top-down
- Programmers
- github
- kubernetes
- GKE
- BubbleSort
- Kotlin
- easy
- LeetCode
- Singleton Pattern
- golang
- mobaXTerm
Archives
- Today
- Total
To Be Developer
[LeetCode] 136. Single Number 본문
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# input Data 는 비어 있지 않는 리스트 형태이다.
# 리스트 안의 원소를 같은 것의 수를 카운트 한 후
# 카운트 숫자가 한 개인 원소의 값을 리턴 시킨다.
# input Data 가 [2, 2, 1] 이라고 가정하고 풀어보자
# 각 숫자들의 정보를 집어 넣을 비어있는 리스트를 준비
lst = []
# 매개변수가 int를 원소로 가지고 있는 List 형태이다.
# 매개변수 nums를 for 문으로 하나하나 원소를 꺼낸다.
for i in nums:
# 리스트 안의 원소를 카운트 할 것이기 때문에
# keys = 'num' : 원소 값, 'cnt' : 'nums' 가 나온 횟수의 정보를 가지고 있는
# dic 이라는 비어있는 딕셔너리 변수를 선언한다.
dic = {'num' : None, 'cnt' : 0 }
# 매개변수의 정보가 처음 나온 것인지 체크하기 위한 int 변수
idLst = 0
# 숫자들의 정보를 가지고 있는 lst를 for 문을 돌린다.
for j in lst:
# 만약 lst 안의 원소의 'num' key 가 i 라면
# 원소의 'cnt'를 1 만큼 증가시킨다.
# 1만큼 증가 되었으니 i 는 한 번이라도 나온 것인 것을 체크할 수 있게 되었다.
if j['num'] == i:
j['cnt']+=1
idLst+=1
# idLst 가 0 이면 처음으로 나온 숫자이기 때문에 i의 정보를 list에 append 시킨다.
if idLst==0:
dic['num'] = i
dic['cnt'] +=1
lst.append(dic)
# 모든 nums 를 검수하였으니 그 결과 정보를 가지고 있는 lst를
# 반복문으로 돌려 'cnt' 값이 1 인 'num' 을 반환시킨다.
for i in lst:
if i['cnt']== 1:
return i['num']
if __name__== '__main__':
nums = [2,2,1]
s1 = Solution().singleNumber(nums)
print(s1)
'알고리즘 > LeetCode' 카테고리의 다른 글
| [LeetCode] 189. Rotate Array.py (0) | 2019.03.08 |
|---|---|
| [LeetCode] 7. Reverse Integer (0) | 2019.03.06 |
| [LeetCode] Two Sum ll - Input array is sorted (0) | 2019.03.05 |
| [LeetCode] 566. Reshape the Matrix (0) | 2019.03.04 |
| [LeetCode] 349. InterSection of Two Arrays (0) | 2019.03.03 |