본문 바로가기

전체 글134

[코딩 테스트] 리트코드 208 - Implement Trie(Prefix Tree) A trie (pronounced as "try") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker. Implement the Trie class: 트라이 구조를 문제에서 요구하는 대로 구현하세요. Trie() Initializes the trie object. void insert(String word) Inserts the string word into the trie. boolean sear.. 2021. 5. 31.
[자료구조] Trie in Python 파이썬으로 구현하는 Trie. 출처 - https://www.youtube.com/watch?v=o6563NNbdtg Trie의 필요성 어떠한 사전이 내가 찾는 문자열을 담고 있는 지 알아본다고 하자. 사전이 담고 있는 문자열들 - "leets", "leeds", "leet" 내가 찾고자 하는 문자열 - "leet" 사전을 구성하는 방법엔 여러 가지가 있을 것이다. 예를 들어, hash table을 사용해, 각 문자열을 해싱해 얻은 키 값에 True 값을 지정해주면 Key Value leets True leeds True leet True 와 같이 될 것이고, 내가 찾는 문자열 leet이 사전에 있는가는 if "leet" in hashTable and hashTable["leet"] 의 Value가 Tr.. 2021. 5. 31.
[코딩 테스트] 리트코드 19 - Remove Nth Node From End of List(Medium) in Python Given the head of a linked list, remove the nth node from the end of the list and return its head 연결 리스트의 head가 주어졌을 때, 연결 리스트의 끝에서 n번째 노드를 삭제하고 연결 리스트의 head를 리턴하세요! Example 1: Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5] Example 2: Input: head = [1], n = 1 Output: [] 풀이 나는 문제에서 말하는 소위 "Two-Pass Solution"으로 풀었다. 처음에 한 포인터로 연결 리스트를 순회하면서 연결 리스트의 길이 (length) 를 알아내고, 두번째 포인터로 (length - n)노드의 .. 2021. 5. 29.
[코딩테스트]리트코드 14 - Longest Common Prefix (Easy) in Python Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". 문자열의 배열이 매개변수로 주어졌을 때, 공통된 가장 긴 접미사를 반환하세요. Example 1: Input: strs = ["flower","flow","flight"] Output: "fl" Constraints: 1 2021. 5. 29.