duanhao9176 2018-10-30 18:31
浏览 14
已采纳

切排序的切片的最佳方法

I want to cut an array of integers before a specified value, and return an array containing those values and the remaining values in the array. We can assume the array is sorted. This is what I have so far:

func cutIntArrBefore(arr1 []int, n int) ([]int, []int) {
    arr2 := make([]int, 0, len(arr1))
    sliceIndex := 0


    for i, num := range arr1 {
        if num < n {
            arr2 = append(arr2, num)
            sliceIndex = i
        }
    }

    sliceIndex = sliceIndex + 1
    if sliceIndex >= len(arr1) {
        return arr2, nil
    } else {
        arr1 := arr1[sliceIndex:]   
        return arr2, arr1
    }
}

test code:

func main() {
    var arr1, arr2, arr3 []int
    arr1 = []int{1,2,3,4,5,6,7,8}
    arr2, arr3 = cutIntArrBefore(arr1, 5)
    fmt.Printf("(%+v) = %+v, %+v
", arr1, arr2, arr3)

    arr1 = []int{1,5}
    arr2, arr3 = cutIntArrBefore(arr1, 5)
    fmt.Printf("(%+v) = %+v, %+v
", arr1, arr2, arr3)

    arr1 = []int{1}
    arr2, arr3 = cutIntArrBefore(arr1, 5)
    fmt.Printf("(%+v) = %+v, %+v
", arr1, arr2, arr3)

    arr1 = []int{5}
    arr2, arr3 = cutIntArrBefore(arr1, 5)
    fmt.Printf("(%+v) = %+v, %+v
", arr1, arr2, arr3)

    arr1 = []int{5,6}
    arr2, arr3 = cutIntArrBefore(arr1, 5)
    fmt.Printf("(%+v) = %+v, %+v
", arr1, arr2, arr3)

    arr1 = []int{5,5}
    arr2, arr3 = cutIntArrBefore(arr1, 5)
    fmt.Printf("(%+v) = %+v, %+v
", arr1, arr2, arr3)

    arr1 = []int{7,7,7}
    arr2, arr3 = cutIntArrBefore(arr1, 5)
    fmt.Printf("(%+v) = %+v, %+v
", arr1, arr2, arr3)
}

output:

([1 2 3 4 5 6 7 8]) = [1 2 3 4], [5 6 7 8]
([1 5]) = [1], [5]
([1]) = [1], []
([5]) = [], []
([5 6]) = [], [6]
([5 5]) = [], [5]
([7 7 7]) = [], [7 7]

Unfortunately, as you can see, if the first element is after the specified value, it gets skipped over. I want to do this as elegantly as possible. I'm hoping there's another way without having to create two arrays, or adding another if statement.

  • 写回答

1条回答 默认 最新

  • duanpei8518 2018-10-30 18:41
    关注

    Your implementation contains an off-by-one error, which can easily be worked around by using the index of the leftmost target value as the pivot point using the slice expressions arr[:i] and arr[i:].

    Also, consider using sort.SearchInts(...) to find the target index in O(lg(n)) time instead of O(n). Using a builtin function will also likely improve legibility and maintainability of the code.

    For example (Go Playground):

    func cutBefore(xs []int, x int) ([]int, []int) {
      i := sort.SearchInts(xs, x)
      return xs[:i], xs[i:]
    }
    
    func main() {
      xss := [][]int{
        {1, 2, 3, 4, 5, 6, 7, 8},
        {1, 5},
        {1},
        {5},
        {5, 6},
        {5, 5},
        {7, 7, 7},
      }
      for _, xs := range xss {
        fmt.Println(cutBefore(xs, 5))
      }
      // [1 2 3 4] [5 6 7 8]
      // [1] [5]
      // [1] []
      // [] [5]
      // [] [5 6]
      // [] [5 5]
      // [] [7 7 7]
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊