如何合并连续的整数

时间:2019-02-17 05:36:02

标签: go

我想知道是否有一个很好的方法可以将两个整数合并,但必须以连续的方式返回true,所以{100,101}可以与{103,104,102}合并,而不能与{100,101}合并,并且{103,104,105}(缺少102)根据问题进行编码。

package main
 
import (
    "fmt"
    "math/rand"
    "time"
)
 
func main() {
 
    slice := generateSlice(20)
    fmt.Println("\n--- Unsorted --- \n\n", slice)
    fmt.Println("\n--- Sorted ---\n\n", mergeSort(slice), "\n")
}
 
// Generates a slice of size, size filled with random numbers
func generateSlice(size int) []int {
 
    slice := make([]int, size, size)
    rand.Seed(time.Now().UnixNano())
    for i := 0; i < size; i++ {
        slice[i] = rand.Intn(999) - rand.Intn(999)
    }
    return slice
}
  
func mergeSort(items []int) []int {
    var num = len(items)
      
    if num == 1 {
        return items
    }
      
    middle := int(num / 2)
    var (
        left = make([]int, middle)
        right = make([]int, num-middle)
    )
    for i := 0; i < num; i++ {
        if i < middle {
            left[i] = items[i]
        } else {
            right[i-middle] = items[i]
        }
    }
      
    return merge(mergeSort(left), mergeSort(right))
}
  
func merge(left, right []int) (result []int) {
    result = make([]int, len(left) + len(right))
      
    i := 0
    for len(left) > 0 && len(right) > 0 {
        if left[0] < right[0] {
            result[i] = left[0]
            left = left[1:]
        } else {
            result[i] = right[0]
            right = right[1:]
        }
        i++
    }
      
    for j := 0; j < len(left); j++ {
        result[i] = left[j]
        i++
    }
    for j := 0; j < len(right); j++ {
        result[i] = right[j]
        i++
    }
      
    return
}

输出:

https://play.golang.org/p/oAtGTiUnxrE

问题:

pumpung是连续整数的排列,可能带有重复项。 如果两个大泵形成较大的大泵,则可以合并。例如, [100,101]和[103,102,104], [222,221,220,219]和[221,222,223,225,224] 可以合并;而 [100,101]和[103,104,105] 不能。 编写一个函数IsMergeable(pumpung1,pumpung2),如果给定返回true Pumpungs可以合并。您可能会认为这些论点确实存在。

1 个答案:

答案 0 :(得分:1)

例如,

package main

import "fmt"

const (
    maxInt = int(^uint(0) >> 1)
    minInt = -maxInt - 1
)

func isMerge(a1, a2 []int) bool {
    min1, max1 := maxInt, minInt
    for _, e1 := range a1 {
        if min1 > e1 {
            min1 = e1
        }
        if max1 < e1 {
            max1 = e1
        }
    }

    for _, e2 := range a2 {
        if e2 == min1-1 || e2 == max1+1 {
            return true
        }
    }

    return false
}

func main() {
    a1 := []int{100, 101}
    a2 := []int{103, 104, 102}
    a3 := []int{103, 104, 105}
    fmt.Println(a1, a2, isMerge(a1, a2))
    fmt.Println(a2, a1, isMerge(a2, a1))
    fmt.Println(a1, a3, isMerge(a1, a3))
    fmt.Println(a3, a1, isMerge(a3, a1))

    a4 := []int{222, 221, 220, 219}
    a5 := []int{221, 222, 223, 225, 224}
    fmt.Println(a4, a5, isMerge(a4, a5))
    fmt.Println(a5, a4, isMerge(a5, a4))
}

游乐场:https://play.golang.org/p/rRGPoivhEWW

输出:

[100 101] [103 104 102] true
[103 104 102] [100 101] true
[100 101] [103 104 105] false
[103 104 105] [100 101] false
[222 221 220 219] [221 222 223 225 224] true
[221 222 223 225 224] [222 221 220 219] true