当前位置: 首页 > news >正文

武汉网站建设公司 中网郑州网络营销学校

武汉网站建设公司 中网,郑州网络营销学校,写真盗版网站怎么做,百度怎么发布店铺信息文章目录 移除指定位置的元素查找元素的位置查找最大最小的元素去重随机打乱排序二维排序sort.Sort 排序 下面的方法省略一些校验,如数组越界等,且都采用泛型(要求go版本 > 1.18) 移除指定位置的元素 package mainimport ("fmt" )func Del…

文章目录

        • 移除指定位置的元素
        • 查找元素的位置
        • 查找最大最小的元素
        • 去重
        • 随机打乱
        • 排序
        • 二维排序
        • sort.Sort 排序

下面的方法省略一些校验,如数组越界等,且都采用泛型(要求go版本 >= 1.18)

移除指定位置的元素

package mainimport ("fmt"
)func Delete[T any](source []T, index int) []T {return append(source[:index], source[index+1:]...)
}func main() {fmt.Println(Delete([]int{1, 2, 3, 4, 5}, 3)) //[1,2,3,5]
}

查找元素的位置

找到返回位置,找不到返回 -1
也可以用来判断某个元素是否存在,大于 -1 即存在

package mainimport ("fmt"
)func Delete[T any](source []T, assert func(t T) bool) int {for index, item := range source {if assert(item) {return index}}return -1
}func main() {fmt.Println(Delete([]int{1, 2, 3, 4, 5}, func(t int) bool {return t == 3}))// 2
}

查找最大最小的元素

package mainimport ("fmt"
)func Find[T any](source []T, assert func(t1, t2 T) bool) T {max := source[0]for _, item := range source {if assert(max, item) {max = item}}return max
}func main() {fmt.Println(Find([]int{1, 2, 22, 8, 11, 3, 4, 5}, func(t1, t2 int) bool {return t2 > t1})) //22fmt.Println(Find([]int{1, 2, 22, 8, 11, 3, 4, 5}, func(t1, t2 int) bool {return t2 < t1})) //1
}

去重

T 使用 comparable 是因为 map 需要一个可比较的类型

package mainimport "fmt"func Uniq[T comparable](collection []T) []T {result := make([]T, 0, len(collection))seen := make(map[T]struct{}, len(collection))for _, item := range collection {if _, ok := seen[item]; ok {continue}seen[item] = struct{}{}result = append(result, item)}return result
}func main() {fmt.Println(Uniq([]int{6, 7, 6, 5})) //[6,7,5]
}

使用 struct{}{} 是因为它不占内存

package mainfunc main() {fmt.Println(unsafe.Sizeof(struct{}{})) // 0fmt.Println(unsafe.Sizeof(true))       // 1
}

随机打乱

这里引入了 i int64,这个是让他们的随机种子不一样,不然在 main中的 10次打印结果都一致

package mainimport ("fmt""math/rand""time"
)func random[T any](input []T, i int64) []T {rand.Seed(time.Now().Unix() + i)for i := len(input) - 1; i >= 0; i-- {index := rand.Intn(i + 1)input[index], input[i] = input[i], input[index]}return input
}func main() {for i := 0; i < 10; i++ {fmt.Println(random([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}, int64(i)))}//下面的结果都一样for i := 0; i < 10; i++ {fmt.Println(random([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}, 0))}
}

排序

这里展示的是选择排序,可以更换其它排序算法

package mainimport "fmt"func sort[T any](source []T, assert func(a, b T) bool) []T {for i := 0; i < len(source); i++ {for j := i + 1; j < len(source); j++ {if assert(source[i], source[j]) {source[j], source[i] = source[i], source[j]}}}return source
}func main() {r := sort([]int{1, 4, 3, 9, 7, 8, 5}, func(a, b int) bool {return a < b})fmt.Println(r) //[9 8 7 5 4 3 1]
}

二维排序

在排序的基础上增加了一个 index(要比较的元素位置),根据二维index位置的元素大小,来决定一维的排序位置

package mainimport "fmt"func TwoDimensionalSort[T any](source [][]T, index int, assert func(a, b T) bool) [][]T {for i := 0; i < len(source); i++ {for j := i + 1; j < len(source); j++ {//二维index位置上元素的比较if assert(source[i][index], source[j][index]) {//一维位置交换source[j], source[i] = source[i], source[j]}}}return source
}func main() {result := TwoDimensionalSort([][]int{{1, 3, 5}, {6, 2, 3}, {5, 9, 1}}, 2, func(a, b int) bool {return a > b})fmt.Println(result) //[[5 9 1] [6 2 3] [1 3 5]]result = TwoDimensionalSort([][]int{{1, 3, 5}, {6, 2, 3}, {5, 9, 1}}, 1, func(a, b int) bool {return a > b})fmt.Println(result) //[[6 2 3] [1 3 5] [5 9 1]]
}

sort.Sort 排序

进阶版本使用泛型加 sor.Sort 处理,算法由 sort.Sort 实现,而我只需要实现它排序对象的接口即可,Len()返回元素数量,Less() 比较方式 i 是否要 排在 j 前面,Swap() 交换方式

type Interface interface {// Len is the number of elements in the collection.Len() int// Less reports whether the element with index i// must sort before the element with index j.//// If both Less(i, j) and Less(j, i) are false,// then the elements at index i and j are considered equal.// Sort may place equal elements in any order in the final result,// while Stable preserves the original input order of equal elements.//// Less must describe a transitive ordering://  - if both Less(i, j) and Less(j, k) are true, then Less(i, k) must be true as well.//  - if both Less(i, j) and Less(j, k) are false, then Less(i, k) must be false as well.//// Note that floating-point comparison (the < operator on float32 or float64 values)// is not a transitive ordering when not-a-number (NaN) values are involved.// See Float64Slice.Less for a correct implementation for floating-point values.Less(i, j int) bool// Swap swaps the elements with indexes i and j.Swap(i, j int)
}
package mainimport ("fmt""sort"
)type SortStruct[T comparable] struct {Data   [][]TAssert func(a, b T) boolIndex  int
}func (s SortStruct[T]) Len() int {return len(s.Data)
}func (s SortStruct[T]) Less(i, j int) bool {return s.Assert(s.Data[i][s.Index], s.Data[j][s.Index])
}func (s SortStruct[T]) Swap(i, j int) {s.Data[i], s.Data[j] = s.Data[j], s.Data[i]
}func main() {result := SortStruct[int]{Data: [][]int{{1, 2, 3},{3, 1, 2},{6, 7, 8},},Assert: func(a, b int) bool {return a < b},Index: 1,}sort.Sort(result)fmt.Println(result.Data) //[[3 1 2] [1 2 3] [6 7 8]]
}
http://www.hkea.cn/news/164148/

相关文章:

  • 3d效果图软件宁波seo行者seo09
  • 美国做按摩广告的网站网站优化教程
  • wordpress云建站教程信息流广告公司一级代理
  • 我有一个域名怎么做网站百度一下下载
  • 郑州网站建设品牌好安装百度到桌面
  • 株洲做网站定制百度灰色词优化排名
  • 上海网页设计公司兴田德润电话排名优化外包公司
  • 做360网站优化快推广普通话宣传语手抄报
  • 动态网站开发语言有哪些大学生创新创业大赛
  • 关键词推广公司网站网络排名优化方法
  • 福州移动网站建设网络营销推广工具有哪些
  • win2008sr怎么用iis做网站国外网站加速
  • 合肥++网站建设磐石网站seo
  • 万网主机怎么上传网站如何在百度上投放广告
  • 做网站时如何给文字做超链接全球疫情最新数据消息
  • 四川省住建厅官方网站3分钟搞定网站seo优化外链建设
  • 做网站阳泉巨量千川广告投放平台
  • 温岭哪里有做网站的如何自制网站
  • 知道创于 wordpress搜索引擎优化宝典
  • 乌兰县wap网站建设公司有效获客的六大渠道
  • 微信网站开发教程视频教程百度一下主页官网
  • 网站开发专业前景关键词挖掘排名
  • 网站开发属于什么职位类别seo查询站长工具
  • wordpress postmetaseoul national university
  • 商务网站的主要存在形式杭州百度快照优化公司
  • 个人备案网站做购物网站可以不班级优化大师免费下载电脑版
  • 贸易网站建设互联网广告代理加盟
  • 深圳网站建设网络公司河北关键词排名推广
  • 在工商网上怎么注册公司seo优化博客
  • 免费的小程序怎么赚钱历下区百度seo