ADD: math.A && math.C

This commit is contained in:
NorthCityChen 2022-05-03 18:52:56 +08:00
parent d00014020c
commit ed3284d5b6
5 changed files with 106 additions and 18 deletions

18
app.go
View File

@ -1,6 +1,6 @@
/* /*
* @Author: NorthCity1984 * @Author: NorthCity1984
* @LastEditTime: 2022-04-26 17:05:06 * @LastEditTime: 2022-05-03 18:51:46
* @Description: * @Description:
* @Website: https://grimoire.cn * @Website: https://grimoire.cn
* Copyright (c) NorthCity1984 All rights reserved. * Copyright (c) NorthCity1984 All rights reserved.
@ -10,18 +10,18 @@ package main
import ( import (
"fmt" "fmt"
"gitee.com/NorthCityChen/stl-go/bit" "gitee.com/NorthCityChen/stl-go/math"
) )
var a = []int{1, 2, 65, 4, 33, 5} var a = []int{1, 2, 65, 4, 33, 5}
func main() { func main() {
ans := a[0] // h := heap.Init(a, heap.MinHeap[int]())
for i := len(a) - 1; i >= 1; i-- { // fmt.Println(h)
a[i] = a[i] - a[i-1]
} ans := math.C(8, 2)
b := bit.Init(a) fmt.Println(ans)
fmt.Println(b)
ans = b.Ask(6) ans = math.A(12, 2)
fmt.Println(ans) fmt.Println(ans)
} }

View File

@ -1,6 +1,6 @@
/* /*
* @Author: NorthCity1984 * @Author: NorthCity1984
* @LastEditTime: 2022-04-17 12:04:32 * @LastEditTime: 2022-04-27 13:31:14
* @Description: * @Description:
* @Website: https://grimoire.cn * @Website: https://grimoire.cn
* Copyright (c) NorthCity1984 All rights reserved. * Copyright (c) NorthCity1984 All rights reserved.
@ -24,6 +24,9 @@ type dequeueNode[T Number] struct {
RPointer *dequeueNode[T] RPointer *dequeueNode[T]
} }
// 返回对应类型的零值 对比 T(Rune(0))在获取空字符串的情况下快14倍左右
func zeroValue[T Number]() (value T) { return }
func Init[T Number]() *Dequeue[T] { func Init[T Number]() *Dequeue[T] {
return &Dequeue[T]{size: 0, leftHead: new(dequeueNode[T]), rightHead: new(dequeueNode[T])} return &Dequeue[T]{size: 0, leftHead: new(dequeueNode[T]), rightHead: new(dequeueNode[T])}
} }
@ -50,7 +53,7 @@ func (q *Dequeue[T]) LPush(val T) bool {
func (q *Dequeue[T]) LPop() (T, bool) { func (q *Dequeue[T]) LPop() (T, bool) {
if q.IsEmpty() { if q.IsEmpty() {
return T(rune(0)), false return zeroValue[T](), false
} }
val := q.leftHead.Val val := q.leftHead.Val
q.leftHead = q.leftHead.RPointer q.leftHead = q.leftHead.RPointer
@ -72,7 +75,7 @@ func (q *Dequeue[T]) RPush(val T) bool {
func (q *Dequeue[T]) RPop() (T, bool) { func (q *Dequeue[T]) RPop() (T, bool) {
if q.IsEmpty() { if q.IsEmpty() {
return T(rune(0)), false return zeroValue[T](), false
} }
val := q.rightHead.Val val := q.rightHead.Val
q.rightHead = q.rightHead.LPointer q.rightHead = q.rightHead.LPointer
@ -82,14 +85,14 @@ func (q *Dequeue[T]) RPop() (T, bool) {
func (q *Dequeue[T]) LNode() (T, bool) { func (q *Dequeue[T]) LNode() (T, bool) {
if q.IsEmpty() { if q.IsEmpty() {
return T(rune(0)), false return zeroValue[T](), false
} }
return q.leftHead.Val, true return q.leftHead.Val, true
} }
func (q *Dequeue[T]) RNode() (T, bool) { func (q *Dequeue[T]) RNode() (T, bool) {
if q.IsEmpty() { if q.IsEmpty() {
return T(rune(0)), false return zeroValue[T](), false
} }
return q.rightHead.Val, true return q.rightHead.Val, true
} }

50
dequeue/dequeue_test.go Normal file
View File

@ -0,0 +1,50 @@
/*
* @Author: NorthCity1984
* @LastEditTime: 2022-04-27 13:36:25
* @Description:
* @Website: https://grimoire.cn
* Copyright (c) NorthCity1984 All rights reserved.
*/
package dequeue
import (
"testing"
// "gitee.com/NorthCityChen/stl-go/dequeue"
)
// type Number interface {
// int | int64 | float32 | float64 | string
// }
// func zeroValue[T Number]() (value T) { return }
func TRune[T Number]() T { return T(rune(0)) }
func BenchmarkZeroValue(b *testing.B) {
for i := 0; i < b.N; i++ {
zeroValue[float64]()
zeroValue[float32]()
zeroValue[int]()
zeroValue[int64]()
zeroValue[string]()
}
}
func BenchmarkTRune(b *testing.B) {
for i := 0; i < b.N; i++ {
TRune[float64]()
TRune[float32]()
TRune[int64]()
TRune[int]()
TRune[string]()
}
}
func BenchmarkRunDequeue(b *testing.B) {
dq := Init[int]()
for i := 0; i < b.N; i++ {
dq.LPush(1)
dq.RPush(2)
dq.LPop()
dq.RPop()
}
}

View File

@ -1,6 +1,6 @@
/* /*
* @Author: NorthCity1984 * @Author: NorthCity1984
* @LastEditTime: 2022-04-26 16:06:24 * @LastEditTime: 2022-04-27 13:19:36
* @Description: * @Description:
* @Website: https://grimoire.cn * @Website: https://grimoire.cn
* Copyright (c) NorthCity1984 All rights reserved. * Copyright (c) NorthCity1984 All rights reserved.
@ -17,6 +17,9 @@ type heap[T Number] struct {
cmp func(T, T) bool cmp func(T, T) bool
} }
// 返回对应类型的零值
func zeroValue[T Number]() (value T) { return }
func (h *heap[T]) up(x int) { func (h *heap[T]) up(x int) {
for x > 1 && h.cmp(h.array[x], h.array[x/2]) { for x > 1 && h.cmp(h.array[x], h.array[x/2]) {
h.array[x], h.array[x/2] = h.array[x/2], h.array[x] h.array[x], h.array[x/2] = h.array[x/2], h.array[x]
@ -46,7 +49,7 @@ func (h *heap[T]) Push(v T) {
func (h *heap[T]) Pop() T { func (h *heap[T]) Pop() T {
if h.len == 0 { if h.len == 0 {
return 0 return zeroValue[T]()
} }
ret := h.array[1] ret := h.array[1]
h.array[1], h.array[h.len] = h.array[h.len], h.array[1] h.array[1], h.array[h.len] = h.array[h.len], h.array[1]
@ -61,8 +64,10 @@ func (h *heap[T]) Top() T {
} }
func Init[T Number](array []T, cmp func(T, T) bool) heap[T] { func Init[T Number](array []T, cmp func(T, T) bool) heap[T] {
newArray := make([]T, len(array)+1)
copy(newArray[1:], array)
h := heap[T]{ h := heap[T]{
array: append([]T{0}, array...), array: newArray,
len: len(array), len: len(array),
cmp: cmp, cmp: cmp,
} }
@ -71,3 +76,15 @@ func Init[T Number](array []T, cmp func(T, T) bool) heap[T] {
} }
return h return h
} }
func MaxHeap[T Number]() func(T, T) bool {
return func(t1, t2 T) bool {
return t1 > t2
}
}
func MinHeap[T Number]() func(T, T) bool {
return func(t1, t2 T) bool {
return t1 < t2
}
}

View File

@ -1,6 +1,6 @@
/* /*
* @Author: NorthCity1984 * @Author: NorthCity1984
* @LastEditTime: 2022-04-03 09:32:48 * @LastEditTime: 2022-05-03 18:50:22
* @Description: * @Description:
* @Website: https://grimoire.cn * @Website: https://grimoire.cn
* Copyright (c) NorthCity1984 All rights reserved. * Copyright (c) NorthCity1984 All rights reserved.
@ -77,3 +77,21 @@ func Sqrt(x float64) float64 {
} }
return res return res
} }
// 排列数: A(3,1)=3
func A(n, m int) int {
res := 1
for i := m; i >= 1; i-- {
res *= n //n × n-1 × n-2 × ... n-mm就是需要减1的次数
n--
}
return res
}
// 组合数C(3, 2)=3
func C(n, m int) int {
if m == 0 {
return 1
}
return C(n-1, m-1) * (n / m)
}