mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
go: allow testing
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
e2b6b15847
commit
d2ef757754
33 changed files with 81 additions and 64 deletions
|
@ -1,4 +1,4 @@
|
|||
package append_characters_to_string_to_make_subsequence
|
||||
package main
|
||||
|
||||
func appendCharacters(s string, t string) int {
|
||||
si, ti := 0, 0
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package continuous_subarray_sum
|
||||
package main
|
||||
|
||||
func checkSubarraySum(nums []int, k int) bool {
|
||||
first_index_of := make(map[int]int)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package count_triplets_that_can_form_two_arrays_of_equal_xor
|
||||
package main
|
||||
|
||||
func countTriplets(arr []int) int {
|
||||
precomputePrefix := func() []int {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package diameter_of_binary_tree
|
||||
package main
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package find_common_characters
|
||||
package main
|
||||
|
||||
func commonChars(words []string) []string {
|
||||
A := int('a')
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package get_equal_substrings_within_budget
|
||||
package main
|
||||
|
||||
func equalSubstring(s string, t string, maxCost int) int {
|
||||
abs := func(x int) int {
|
||||
|
|
11
go/go.mod
Normal file
11
go/go.mod
Normal file
|
@ -0,0 +1,11 @@
|
|||
module gitlab.com/mfocko/LeetCode
|
||||
|
||||
go 1.22.3
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/emirpasic/gods/v2 v2.0.0-alpha // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/stretchr/testify v1.9.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
11
go/go.sum
Normal file
11
go/go.sum
Normal file
|
@ -0,0 +1,11 @@
|
|||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/emirpasic/gods/v2 v2.0.0-alpha h1:dwFlh8pBg1VMOXWGipNMRt8v96dKAIvBehtCt6OtunU=
|
||||
github.com/emirpasic/gods/v2 v2.0.0-alpha/go.mod h1:W0y4M2dtBB9U5z3YlghmpuUhiaZT2h6yoeE+C1sCp6A=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
@ -1,4 +1,4 @@
|
|||
package height_checker
|
||||
package main
|
||||
|
||||
func heightChecker(heights []int) int {
|
||||
// count the heights
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package implement_queue_using_stacks
|
||||
package main
|
||||
|
||||
import (
|
||||
"slices"
|
||||
|
@ -9,13 +9,18 @@ type MyQueue struct {
|
|||
asStack []int
|
||||
}
|
||||
|
||||
func Constructor() MyQueue {
|
||||
func NewQueue() MyQueue {
|
||||
return MyQueue{
|
||||
asQueue: make([]int, 0, 16),
|
||||
asStack: make([]int, 0, 16),
|
||||
}
|
||||
}
|
||||
|
||||
// [XXX] Uncomment for LeetCode
|
||||
// func Constructor() MyQueue {
|
||||
// return NewQueue()
|
||||
// }
|
||||
|
||||
func (this *MyQueue) Push(x int) {
|
||||
this.asStack = append(this.asStack, x)
|
||||
}
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
package implement_queue_using_stacks
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEmpty(t *testing.T) {
|
||||
obj := Constructor()
|
||||
func Test_ImplementQueueUsingStacks_Empty(t *testing.T) {
|
||||
obj := NewQueue()
|
||||
|
||||
if !obj.Empty() {
|
||||
t.Error("Queue should be empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBasic(t *testing.T) {
|
||||
q := Constructor()
|
||||
func Test_ImplementQueueUsingStacks_Basic(t *testing.T) {
|
||||
q := NewQueue()
|
||||
|
||||
q.Push(1)
|
||||
q.Push(2)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package implement_trie_prefix_tree
|
||||
package main
|
||||
|
||||
type TrieNode struct {
|
||||
has bool
|
||||
|
@ -9,10 +9,15 @@ type Trie struct {
|
|||
root TrieNode
|
||||
}
|
||||
|
||||
func Constructor() Trie {
|
||||
func NewTrie() Trie {
|
||||
return Trie{}
|
||||
}
|
||||
|
||||
// [XXX] Uncomment for LeetCode
|
||||
// func Constructor() Trie {
|
||||
// return NewTrie()
|
||||
// }
|
||||
|
||||
func (this *Trie) Insert(word string) {
|
||||
node := &this.root
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package ipo
|
||||
package main
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
|
|
6
go/listnode.go
Normal file
6
go/listnode.go
Normal file
|
@ -0,0 +1,6 @@
|
|||
package main
|
||||
|
||||
type ListNode struct {
|
||||
Val int
|
||||
Next *ListNode
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package longest_palindrome
|
||||
package main
|
||||
|
||||
func longestPalindrome(s string) int {
|
||||
getFreqs := func() map[rune]int {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package middle_of_the_linked_list
|
||||
package main
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package minimum_increment_to_make_array_unique
|
||||
package main
|
||||
|
||||
import "slices"
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package minimum_number_of_moves_to_seat_everyone
|
||||
package main
|
||||
|
||||
import (
|
||||
"slices"
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package number_of_steps_to_reduce_a_number_in_binary_representation_to_one
|
||||
package main
|
||||
|
||||
func numSteps(s string) int {
|
||||
steps := 0
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package palindrome_partitioning
|
||||
package main
|
||||
|
||||
import (
|
||||
"slices"
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package palindromic_substrings
|
||||
package main
|
||||
|
||||
func checkSubstring(s string, i, j int) int {
|
||||
if i < 0 || j >= len(s) {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package relative_sort_array
|
||||
package main
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package remove_nth_node_from_end_of_list
|
||||
package main
|
||||
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package same_tree
|
||||
package main
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package score_of_a_string
|
||||
package main
|
||||
|
||||
func scoreOfString(s string) int {
|
||||
abs := func(x int) int {
|
||||
|
|
|
@ -1,32 +1,4 @@
|
|||
package search_suggestions_system
|
||||
|
||||
type TrieNode struct {
|
||||
has bool
|
||||
successors [26]*TrieNode
|
||||
}
|
||||
|
||||
type Trie struct {
|
||||
root TrieNode
|
||||
}
|
||||
|
||||
func Constructor() Trie {
|
||||
return Trie{}
|
||||
}
|
||||
|
||||
func (this *Trie) Insert(word string) {
|
||||
node := &this.root
|
||||
|
||||
for _, c := range word {
|
||||
idx := int(c) - int('a')
|
||||
|
||||
if node.successors[idx] == nil {
|
||||
node.successors[idx] = new(TrieNode)
|
||||
}
|
||||
node = node.successors[idx]
|
||||
}
|
||||
|
||||
node.has = true
|
||||
}
|
||||
package main
|
||||
|
||||
func (this *Trie) Suggest(word string) [][]string {
|
||||
firstThree := func(node *TrieNode, prefix string) []string {
|
||||
|
@ -71,7 +43,7 @@ func (this *Trie) Suggest(word string) [][]string {
|
|||
}
|
||||
|
||||
func suggestedProducts(products []string, searchWord string) [][]string {
|
||||
trie := Constructor()
|
||||
trie := NewTrie()
|
||||
for _, product := range products {
|
||||
trie.Insert(product)
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package single_number_iii
|
||||
package main
|
||||
|
||||
func singleNumber(nums []int) []int {
|
||||
reduce := func() int {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package sort_colors
|
||||
package main
|
||||
|
||||
func sortColors(nums []int) {
|
||||
l, r := 0, len(nums)-1
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package special_array_with_x_elements_greater_than_or_equal_x
|
||||
package main
|
||||
|
||||
import (
|
||||
"slices"
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package subarray_sums_divisible_by_k
|
||||
package main
|
||||
|
||||
func subarraysDivByK(nums []int, k int) int {
|
||||
counters := make([]int, k)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package subsets
|
||||
package main
|
||||
|
||||
func subsets(nums []int) [][]int {
|
||||
var sets [][]int
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package the_number_of_beautiful_subsets
|
||||
package main
|
||||
|
||||
import (
|
||||
"slices"
|
||||
|
|
7
go/treenode.go
Normal file
7
go/treenode.go
Normal file
|
@ -0,0 +1,7 @@
|
|||
package main
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
Loading…
Reference in a new issue