小编Pkz*_*zzz的帖子

如何使用goroutine来执行for循环?

假设我有一个像这样的切片:

stu = [{"id":"001","name":"A"} {"id":"002", "name":"B"}]也许还有更多这样的元素。切片内部是一个长字符串,我想使用 json.unmarshal 来解析它。

type Student struct {
   Id   string `json:"id"`
   Name string `json:"name"`
}

studentList := make([]Student,len(stu))
for i, st := range stu {
   go func(st string){
      studentList[i], err = getList(st)
      if err != nil {
         return ... //just example
      }
   }(st)
}
//and a function like this
func getList(stu string)(res Student, error){
   var student Student
   err := json.Unmarshal(([]byte)(stu), &student)
   if err != nil {
      return
   }
   return &student,nil
}
Run Code Online (Sandbox Code Playgroud)

我得到了 nil 结果,所以我想说 goroutine …

go goroutine

1
推荐指数
1
解决办法
1万
查看次数

k个数字和N个元素的最快排序方法,k <<< N

问题:有n个球,标记为0、1、2,顺序乱,我想从小到大排序。球:

1, 2, 0, 1, 1, 2, 2, 0, 1, 2, ...
Run Code Online (Sandbox Code Playgroud)

一定要用最快的方式来解决,不能用sort()函数,我想了很多办法,比如冒泡排序,插入排序等等,但是速度不快。是否有一种算法使时间复杂度为 O(logn) 或 O(n)?

给定的球列表 A[] 和长度 n

void sortBalls(int A[], int n)
{
 //code here
}
Run Code Online (Sandbox Code Playgroud)

c++ sorting

0
推荐指数
1
解决办法
65
查看次数

标签 统计

c++ ×1

go ×1

goroutine ×1

sorting ×1