Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HasCycle ¶
141. Linked List Cycle
Given head, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter. Return true if there is a cycle in the linked list. Otherwise, return false.
Runtime: 20 ms, faster than 22.46% of Go online submissions for Linked List Cycle. Memory Usage: 6.4 MB, less than 17.18% of Go online submissions for Linked List Cycle.
Types ¶
type DoublyNode ¶
type DoublyNode struct { Val int Next *DoublyNode Prev *DoublyNode }
type LRUCache ¶
146. LRU Cache
Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. Implement the LRUCache class:
- LRUCache(int capacity) Initialize the LRU cache with positive size capacity.
- int get(int key) Return the value of the key if the key exists, otherwise return -1.
- void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key.
The functions get and put must each run in O(1) average time complexity.
Runtime: 1053 ms, faster than 28.52% of Go online submissions for LRU Cache. Memory Usage: 73.6 MB, less than 75.90% of Go online submissions for LRU Cache.
func Constructor ¶
type LRUQueue ¶
type LRUQueue struct { Head *DoublyNode Tail *DoublyNode // contains filtered or unexported fields }
func NewLRUQueue ¶
func NewLRUQueue() *LRUQueue
type Node ¶
138. Copy List with Random Pointer
A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null. Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list. For example, if there are two nodes X and Y in the original list, where X.random --> Y, then for the corresponding two nodes x and y in the copied list, x.random --> y. Return the head of the copied linked list. The linked list is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where: * val: an integer representing Node.val * random_index: the index of the node (range from 0 to n-1) that the random pointer points to, * or null if it does not point to any node. Your code will only be given the head of the original linked list.
Runtime: 0 ms, faster than 100.00% of Go online submissions for Copy List with Random Pointer. Memory Usage: 3.7 MB, less than 13.85% of Go online submissions for Copy List with Random Pointer.
type Trie ¶
type Trie struct {
// contains filtered or unexported fields
}
func Constructor3 ¶
func Constructor3() Trie
func (*Trie) StartsWith ¶
type TrieNode ¶
type TrieNode struct {
// contains filtered or unexported fields
}
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 search(String word) Returns true if the string word is in the trie (i.e., was inserted before), and false otherwise.
- boolean startsWith(String prefix) Returns true if there is a previously inserted string word that has the prefix prefix, and false otherwise.
Runtime: 57 ms, faster than 94.05% of Go online submissions for Implement Trie (Prefix Tree). Memory Usage: 18.6 MB, less than 27.50% of Go online submissions for Implement Trie (Prefix Tree).
func NewTrieNode ¶
func NewTrieNode() *TrieNode