- 파이썬, java 기초를 배우고, 예제를 풀었다.
- 알고리즘 공부를 기초부터 진행 중이다.
- 특히 무릎을 탁 쳤던 예제 3개를 선택했다.
1) 링크드 리스트 구현 예제
class Node():
def __init__(self,data):
self.data = data
self.next = None
class LinkedList():
def __init__(self, data):
self.head = Node(data)
def append(self, data):
if self.head is None:
self.head = Node(data)
return
cur = self.head
while cur.next is not None:
cur = cur.next
cur.next = Node(data)
def prin_all(self):
cur = self.head
while cur is not None:
print(cur.data)
cur = cur.next
linked_list = LinkedList(3)
linked_list.append(4)
linked_list.append(5)
linked_list.prin_all()
2) 문자열 뒤집기 예제
input = "011110"
def find_count_to_turn_out_to_all_zero_or_all_one(string):
count_to_all_zero = 0
count_to_all_one = 0
if string[0] == '0':
count_to_all_one += 1
elif string[0] == '1':
count_to_all_zero += 1
for i in range(len(string) - 1):
if string[i] != string[i + 1]:
if string[i + 1] == '0':
count_to_all_one += 1
if string[i + 1] == '1':
count_to_all_zero += 1
return min(count_to_all_one, count_to_all_zero)
result = find_count_to_turn_out_to_all_zero_or_all_one(input)
print(result)
3) 버블 정렬 예제
input = [4, 6, 2, 9, 1]
def bubble_sort(arrays):
n = len(arrays)
for i in range(n-1):
for j in range(n-i-1):
if arrays[j] > arrays[j+1]:
arrays[j],arrays[j+1] = arrays[j+1],arrays[j]
return arrays
result = bubble_sort(input)
print(result) # [1, 2, 4, 6, 9] 가 되어야 합니다!
- 자료구조 ( Stack, Queue, Hash에 대한 전반적 개념 )
- 재귀함수
- 탐색방법( 이진탐색... )
- 정렬방법( 버블정렬.. )
1) 생각하는 힘이 필요하다.
2) 기초를 단단하게 하자.