Skip to content

Instantly share code, notes, and snippets.

View maxclav's full-sized avatar

Maxime Clavel maxclav

View GitHub Profile
@maxclav
maxclav / binarytree-traversals-bfs.go
Last active July 22, 2023 19:38
Binary Tree Breadth-First Search (BFS) Traversal using a queue in Go (GoLang).
package binarytree
// Tree is a binary tree.
type Tree struct {
Root *Node
}
// Node is a binary tree node.
type Node struct {
Val int
@maxclav
maxclav / binarytree-traversals-bfs-zigzag-levelorder.go
Last active July 25, 2023 02:46
Binary Tree Breadth-First Search (BFS) Zigzag Level Order Traversals in Go (GoLang).
package binarytree
// Tree is a binary tree.
type Tree struct {
Root *Node
}
// Node is a binary tree node.
type Node struct {
Val int
@maxclav
maxclav / binarytree-traversals-bfs-levelorder.go
Last active September 19, 2022 01:19
Binary Tree Breadth-First Search (BFS) Level Order Traversals in Go (GoLang).
package binarytree
// Tree is a binary tree.
type Tree struct {
Root *Node
}
// Node is a binary tree node.
type Node struct {
Val int
@maxclav
maxclav / binarytree-traversals-dfs.go
Last active September 19, 2022 01:40
Binary Tree Depth-First Search (DFS) Traversals in Go (GoLang).
package binarytree
// Tree is a binary tree.
type Tree struct {
Root *Node
}
// Node is a binary tree node.
type Node struct {
Val int
@maxclav
maxclav / string-anagram.go
Last active September 18, 2022 18:14
Check if two string variables are anagram in Go (GoLang).
package strings
// AreAnagram returns whether string `s`
// is an anagram of string `t` (true) or not (false).
func AreAnagram(s string, t string) bool {
if len(s) != len(t) {
return false
}
set1, set2 := buildMultiSets(s, t)
return set1.isEqualTo(set2)
@maxclav
maxclav / search-matrix.go
Last active July 24, 2023 16:53
Search a target value on a n*m matrix in Go (GoLang) with three different approaches.
package matrix
// BinarySearch searches `target` in `matrix`
// with the "binary search" algorithm.
// It returns its position if found or `-1, -1` if not found.
func BinarySearch(matrix [][]int, target int) (int, int) {
if len(matrix) == 0 || len(matrix[0]) == 0 {
return -1, -1
}
@maxclav
maxclav / binarysearch.go
Last active September 18, 2022 18:14
Simple binary search on a slice (array) in Go (GoLang).
package slice
// BinarySearch searches a `target` value in `nums`
// using the "binary search" algorithm.
// It returns its position if found or -1 if not found.
func BinarySearch(nums []int, target int) int {
if len(nums) == 0 {
return -1
}
@maxclav
maxclav / bubblesort.go
Last active July 24, 2023 16:53
Simple and custom bubble sort in Go (GoLang).
package slice
// For learning purpose only.
// Please, use the sort package of Go's Standard library: https://pkg.go.dev/sort
// BubbleSort sorts the slice `vals` with the
// bubble sort algorithm.
// Time: O(n^2)
// Space: O(1)
func BubbleSort(vals []int) {
@maxclav
maxclav / insertionsort.go
Last active September 18, 2022 18:14
Simple and custom insertion sort in Go (GoLang).
package slice
// For learning purpose only.
// Please, use the sort package of Go's Standard library: https://pkg.go.dev/sort
// InsertionSort sorts the slice `vals` with the
// insertion sort algorithm.
// Time: O(n^2)
// Space: O(1)
func InsertionSort(vals []int) {
@maxclav
maxclav / selectionsort.go
Last active September 18, 2022 18:14
Simple and custom selection sort in Go (GoLang).
package slice
// For learning purpose only.
// Please, use the sort package of Go's Standard library: https://pkg.go.dev/sort
// SelectionSort sorts the slice `vals` with the
// selection sort algorithm.
// Time: O(n^2)
// Space: O(1)
func SelectionSort(vals []int) {