Merge pull request #126 from msk397/master
function PrintSlice supports Generics
This commit is contained in:
commit
6a380b4157
@ -8,12 +8,12 @@ import (
|
|||||||
. "github.com/krahets/hello-algo/pkg"
|
. "github.com/krahets/hello-algo/pkg"
|
||||||
)
|
)
|
||||||
|
|
||||||
// linerSearchArray 线性查找(数组)
|
/* 线性查找(数组) */
|
||||||
func linerSearchArray(nums []int, target int) int {
|
func linerSearchArray(nums []int, target int) int {
|
||||||
// 遍历数组
|
// 遍历数组
|
||||||
for i := 0; i < len(nums); i++ {
|
for i := 0; i < len(nums); i++ {
|
||||||
|
// 找到目标元素,返回其索引
|
||||||
if nums[i] == target {
|
if nums[i] == target {
|
||||||
// 找到目标元素,返回其索引
|
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -21,10 +21,11 @@ func linerSearchArray(nums []int, target int) int {
|
|||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
// linerSearchLinkedList 线性查找(链表)
|
/* 线性查找(链表) */
|
||||||
func linerSearchLinkedList(node *ListNode, target int) *ListNode {
|
func linerSearchLinkedList(node *ListNode, target int) *ListNode {
|
||||||
// 遍历链表
|
// 遍历链表
|
||||||
for node != nil {
|
for node != nil {
|
||||||
|
// 找到目标元素,返回其索引
|
||||||
if node.Val == target {
|
if node.Val == target {
|
||||||
return node
|
return node
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func PrintSlice(nums []int) {
|
// PrintSlice Print a slice
|
||||||
|
func PrintSlice[T any](nums []T) {
|
||||||
fmt.Printf("%v", nums)
|
fmt.Printf("%v", nums)
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,18 @@ comments: true
|
|||||||
=== "Go"
|
=== "Go"
|
||||||
|
|
||||||
```go title="linear_search.go"
|
```go title="linear_search.go"
|
||||||
|
/* 线性查找(数组) */
|
||||||
|
func linerSearchArray(nums []int, target int) int {
|
||||||
|
// 遍历数组
|
||||||
|
for i := 0; i < len(nums); i++ {
|
||||||
|
// 找到目标元素,返回其索引
|
||||||
|
if nums[i] == target {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 未找到目标元素,返回 -1
|
||||||
|
return -1
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "JavaScript"
|
=== "JavaScript"
|
||||||
@ -138,7 +149,19 @@ comments: true
|
|||||||
=== "Go"
|
=== "Go"
|
||||||
|
|
||||||
```go title="linear_search.go"
|
```go title="linear_search.go"
|
||||||
|
/* 线性查找(链表)*/
|
||||||
|
func linerSearchLinkedList(node *ListNode, target int) *ListNode {
|
||||||
|
// 遍历链表
|
||||||
|
for node != nil {
|
||||||
|
// 找到目标结点,返回之
|
||||||
|
if node.Val == target {
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
node = node.Next
|
||||||
|
}
|
||||||
|
// 未找到目标元素,返回 nil
|
||||||
|
return nil
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
=== "JavaScript"
|
=== "JavaScript"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user