Swift 查找最近的最长组合

Swift 查找最近的最长组合,swift,Swift,我有一个包含整数[1,2,3,7,13,11,4]和整数值12的数组。我需要返回新数组中整数值总和的最接近组合的数组 例如:[1,2,3,7,13,11,4]的值为12。需要返回的数组是[1,2,3,4],因为元素1+2+3+4之和我不需要精通算法,但我会这样做: 因为你的规则是[1,2,3,4,7,11,13] 然后,由于不需要大于5的整数,您可以将数组分成两部分,只取第一部分:[1、2、3、4] 从那里你必须加上4+3,并与你的价值相匹配。如果它不起作用,做4+2,然后4+1。如果仍然大于5

我有一个包含整数[1,2,3,7,13,11,4]和整数值12的数组。我需要返回新数组中整数值总和的最接近组合的数组


例如:[1,2,3,7,13,11,4]的值为12。需要返回的数组是[1,2,3,4],因为元素1+2+3+4之和我不需要精通算法,但我会这样做:

因为你的规则是[1,2,3,4,7,11,13] 然后,由于不需要大于5的整数,您可以将数组分成两部分,只取第一部分:[1、2、3、4] 从那里你必须加上4+3,并与你的价值相匹配。如果它不起作用,做4+2,然后4+1。如果仍然大于5,则使用3,2循环;3,1 ; 等等,我迅速地做了这样的事情:

// Init
let array = [1, 2, 3, 7, 13, 11, 4]
let value = 5
let sortedArray = array.sorted()
let usefulArray = sortedArray.filter() {$0 < value}
var hasCombination = false
var currentIndex = usefulArray.count - 1
var indexForSum = currentIndex - 1

// Process
if currentIndex > 0 {
  hasCombination = true
  while usefulArray[currentIndex] + usefulArray[indexForSum] > value {
  indexForSum -= 1
  if indexForSum < 0 {
    currentIndex -= 1
    indexForSum = currentIndex - 1
  } 
  if currentIndex == 0 {
    hasCombination = false
    break
  }
 }
}

// Result
if hasCombination {
 let combination = [usefulArray[indexForSum], usefulArray[currentIndex]]
} else {
  print("No combination")
}
//初始化
让数组=[1,2,3,7,13,11,4]
设值=5
让sortedArray=array.sorted()
让usefulArray=sortedArray.filter(){$00{
hasscombination=true
而usefulArray[currentIndex]+usefulArray[indexForSum]>值{
指数FORSUM-=1
如果indexForSum<0{
当前索引-=1
indexForSum=currentIndex-1
} 
如果currentIndex==0{
hasscombination=false
打破
}
}
}
//结果
如果有组合{
让组合=[usefulArray[indexForSum],usefulArray[currentIndex]]
}否则{
打印(“无组合”)
}

我想它行得通,如果不行就告诉我

我没有必要精通算法,但我会这样做:

因为你的规则是[1,2,3,4,7,11,13] 然后,由于不需要大于5的整数,您可以将数组分成两部分,只取第一部分:[1、2、3、4] 从那里你必须加上4+3,并与你的价值相匹配。如果它不起作用,做4+2,然后4+1。如果仍然大于5,则使用3,2循环;3,1 ; 等等,我迅速地做了这样的事情:

// Init
let array = [1, 2, 3, 7, 13, 11, 4]
let value = 5
let sortedArray = array.sorted()
let usefulArray = sortedArray.filter() {$0 < value}
var hasCombination = false
var currentIndex = usefulArray.count - 1
var indexForSum = currentIndex - 1

// Process
if currentIndex > 0 {
  hasCombination = true
  while usefulArray[currentIndex] + usefulArray[indexForSum] > value {
  indexForSum -= 1
  if indexForSum < 0 {
    currentIndex -= 1
    indexForSum = currentIndex - 1
  } 
  if currentIndex == 0 {
    hasCombination = false
    break
  }
 }
}

// Result
if hasCombination {
 let combination = [usefulArray[indexForSum], usefulArray[currentIndex]]
} else {
  print("No combination")
}
//初始化
让数组=[1,2,3,7,13,11,4]
设值=5
让sortedArray=array.sorted()
让usefulArray=sortedArray.filter(){$00{
hasscombination=true
而usefulArray[currentIndex]+usefulArray[indexForSum]>值{
指数FORSUM-=1
如果indexForSum<0{
当前索引-=1
indexForSum=currentIndex-1
} 
如果currentIndex==0{
hasscombination=false
打破
}
}
}
//结果
如果有组合{
让组合=[usefulArray[indexForSum],usefulArray[currentIndex]]
}否则{
打印(“无组合”)
}

我想它行得通,如果不行就告诉我

如果数组中没有重复的数字,则此解决方案有效

var array = [1, 2, 3, 7, 13, 11, 4, -12, 22, 100]
print(array.sorted())
let value = 19

func close(array: [Int], value: Int) -> (Int , Int) {
    let sortedArray = array.sorted()
    var lastNumberBeforValue = sortedArray.first!
    for (index,number) in sortedArray.enumerated() {
        let sub = value - number
        if sub > 0 {
            lastNumberBeforValue = number
        }
        if sortedArray.contains(sub) && sub != number {
            return (sub, number)
        } else if index == sortedArray.count - 1 {
            if sub < 0 {
                let near = close(array: array, value: value - lastNumberBeforValue)
                return (near.0, lastNumberBeforValue)
            }
            let near = close(array: array, value: sub)
            return (near.0, number)
        }
    }
    return (-1,-1)
}

let numbers = close(array: array, value: value)
print(numbers) //prints (4, 13)
var数组=[1,2,3,7,13,11,4,-12,22100]
打印(array.sorted())
设值=19
func close(数组:[Int],值:Int)->(Int,Int){
让sortedArray=array.sorted()
var lastnumberforvalue=sortedArray.first!
SorterDarray.enumerated()中的(索引、编号){
设sub=值-数字
如果sub>0{
lastNumberBeforValue=编号
}
如果SorterDarray.contains(sub)&&sub!=编号{
返回(sub,编号)
}如果索引==sortedArray.count-1,则为else{
如果sub<0{
let near=close(数组:数组,值:value-lastNumberBeforValue)
返回值(接近.0,最后一个数字在右值之前)
}
let near=close(数组:数组,值:sub)
返回(接近0,数字)
}
}
返回(-1,-1)
}
让数字=关闭(数组:数组,值:值)
打印(数字)//打印(4,13)

如果数组中没有重复的数字,则此解决方案有效

var array = [1, 2, 3, 7, 13, 11, 4, -12, 22, 100]
print(array.sorted())
let value = 19

func close(array: [Int], value: Int) -> (Int , Int) {
    let sortedArray = array.sorted()
    var lastNumberBeforValue = sortedArray.first!
    for (index,number) in sortedArray.enumerated() {
        let sub = value - number
        if sub > 0 {
            lastNumberBeforValue = number
        }
        if sortedArray.contains(sub) && sub != number {
            return (sub, number)
        } else if index == sortedArray.count - 1 {
            if sub < 0 {
                let near = close(array: array, value: value - lastNumberBeforValue)
                return (near.0, lastNumberBeforValue)
            }
            let near = close(array: array, value: sub)
            return (near.0, number)
        }
    }
    return (-1,-1)
}

let numbers = close(array: array, value: value)
print(numbers) //prints (4, 13)
var数组=[1,2,3,7,13,11,4,-12,22100]
打印(array.sorted())
设值=19
func close(数组:[Int],值:Int)->(Int,Int){
让sortedArray=array.sorted()
var lastnumberforvalue=sortedArray.first!
SorterDarray.enumerated()中的(索引、编号){
设sub=值-数字
如果sub>0{
lastNumberBeforValue=编号
}
如果SorterDarray.contains(sub)&&sub!=编号{
返回(sub,编号)
}如果索引==sortedArray.count-1,则为else{
如果sub<0{
let near=close(数组:数组,值:value-lastNumberBeforValue)
返回值(接近.0,最后一个数字在右值之前)
}
let near=close(数组:数组,值:sub)
返回(接近0,数字)
}
}
返回(-1,-1)
}
让数字=关闭(数组:数组,值:值)
打印(数字)//打印(4,13)

此解决方案还可以在数组中查找两个整数,其总和最接近给定值:

let array = [1, 2, 3, 7, 13, 11, 4]
let value = 5

var difference = Int.max
var result = [Int(), Int()]

for firstInt in array {
    for secondInt in array {
        let tempDifference = abs((firstInt + secondInt) - value)
        if tempDifference < difference {
            difference = tempDifference
            result[0] = firstInt
            result[1] = secondInt
        }
    }
}
let数组=[1,2,3,7,13,11,4]
设值=5
var差=Int.max
var result=[Int(),Int()]
对于数组中的firstInt{
对于数组中的secondInt{
设tempDifference=abs((firstInt+secondInt)-值)
如果温差<温差{
温差=温差
结果[0]=firstInt
结果[1]=secondInt
}
}
}
或不允许多次使用同一值的解决方案:

for (firstIndex, firstInt) in array.enumerated() {
    for (secondIndex, secondInt) in array.enumerated() {
        guard firstInt != secondInt else { break }
        let tempDifference = abs((firstInt + secondInt) - value)
        if tempDifference < difference {
            difference = tempDifference
            result[0] = firstInt
            result[1] = secondInt
        }
    }
}
数组中(firstIndex,firstInt)的
{
用于数组中的(secondIndex,secondInt)。枚举(){
guard firstInt!=secondInt else{break}
设tempDifference=abs((firstInt+secondInt)-值)
如果温差<温差{
温差=温差
结果[0]=firstInt
结果[1]=secondInt
}
}
}
如果您想要更长的结果数组,可以继续嵌套循环并保护同一索引的多次使用。最后,您可以获取具有最大result.count的有效数组。但这不是一个很好的解决方案,因为它计算量很大,并且要求数组具有静态长度