如何搜索数组中的最小正值?

时间:2015-09-17 19:25:59

标签: arrays swift for-in-loop

在我的代码中,checkTheNextTime()函数的数组包含字符串00.00到23.59。通过编写此功能,我想找到最近的未来时间。但是当我尝试使用timeTable(在代码中显示)时,它返回23.30而不是23.32(现在是22.24)。我猜编译器从右到左搜索数组。我怎样才能找到最近的未来时间?

var timeTable = ["09.00","10.20","10.35","11.55","12.00","12.40","13.20","14.40","14.50", "23.00", "23.30", "23.31", "23.32"]
func checkTheNextTime(array array: Array<String>) -> String{


    var nextTime: String?
    for index in array {
        let generatedString:String = getTimeAsMinToCheck(finalTime: index)
        let indexInt = Int(generatedString)
        if indexInt > 0{
            nextTime = index
        }

    }

    return nextTime!
}


func getTimeAsMinToCheck(finalTime finalTime: String) -> String{

        let date = NSDate()
        let formatter = NSDateFormatter()
        formatter.timeStyle = NSDateFormatterStyle.NoStyle
        formatter.dateStyle = NSDateFormatterStyle.ShortStyle
        let now = formatter.stringFromDate(date)
        formatter.locale = NSLocale.systemLocale()
        formatter.dateFormat = "M/dd/yy HH.mm"
        let datetofinish = formatter.dateFromString("\(now) \(finalTime)")
        let finishDate: NSDate = datetofinish!
        let secondsFromNowToFinish = finishDate.timeIntervalSinceNow
        let minutes = Int(secondsFromNowToFinish / 60)
        return String(minutes)
    }

2 个答案:

答案 0 :(得分:1)

此代码应符合您的要求: 完成 Swift 2.0

    var timeTable = ["09.00","10.20","10.35","11.55","12.00","12.40","13.20","14.40","14.50", "23.00", "23.30", "23.31", "23.32"]

    func checkTheNextTime(array array: Array<String>) -> String{ 

        let currentTime:String = getTimeAsMinToCheck(finalTime: "23.24") // set this value by calculating from current time
        let currentTimeInt = Int(currentTime)// Int value of currentTime

        var nextTime: String? //this will hold the nearest future value

        var minDiff: Int = 24*60 //lets start with maximum value

        for index in timeTable {

            let generatedString:String = getTimeAsMinToCheck(finalTime: index)
            let indexInt = Int(generatedString)

            if (indexInt > currentTimeInt) { //checking for future time only

                let timeDiff = indexInt - currentTimeInt // this will be positive

                if (timeDiff < minDiff) {

                    minDiff = timeDiff //update minDiff as timeDiff is less than minDiff
                    nextTime = index
                }
            }
        }

        return nextTime!
    }

答案 1 :(得分:1)

假设23是正确的答案(从上面的评论中不清楚),这里有一个使用swift 2.0和闭包的解决方案

  1. 将timeTable数组映射到当前的delta数组中 时间(无效条目映射到0)

  2. 将最小增量添加到现在的时间

  3. let timeNow: Float = 22.24
    let timeTable = ["09.00","10.20","10.35","11.55","12.00","12.40","13.20","14.40","14.50", "23.00", "23.30", "23.31", "23.32"]
    
    let minDelta = timeTable
      .map { Float(NSNumberFormatter().numberFromString($0) ?? 0.0) - timeNow }
      .filter { $0 > 0 }
      .minElement()
    let nextTime = (minDelta ?? 0) + timeNow
    
    print(nextTime)  // 23.0