Skip to content

Instantly share code, notes, and snippets.

View maxclav's full-sized avatar

Maxime Clavel maxclav

View GitHub Profile
@maxclav
maxclav / tinyURL.go
Last active September 18, 2022 18:15
Simple Key Generation (using base62 encoded hash) for Tiny URL in Go (GoLang).
package tinuURL
import (
"crypto/md5"
"fmt"
"time"
"github.com/google/uuid"
"github.com/jxskiss/base62"
)
@maxclav
maxclav / invert-binarytree.go
Last active July 24, 2023 20:29
Invert Binary Tree in Go (GoLang).
package binarytree
// Tree is a binary tree.
type Tree struct {
Root *Node
}
// Invert reverses the binary tree.
func Invert(tree *Tree) {
_ = InvertFromNode(tree.Root)
@maxclav
maxclav / quicksort.go
Last active September 18, 2022 18:14
Simple and custom quicksort in Go (GoLang).
package slice
// For learning purpose only.
// Please, use the sort package of Go's Standard library: https://pkg.go.dev/sort
// Quicksort sorts the slice `vals` with the
// quicksort algorithm.
// Time: O(nlogn)
// Space: O(logn)
func Quicksort(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) {
@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 / 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 / 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 / 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 / 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 / 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