task_number_count-swift

2017-08-01  本文已影响0人  空降哥伦比亚
func countTaskOfNumber(n: Int, m: Int , start: Array<Int>, end: Array<Int>,query:Array<Int>)  -> Array<Int>? {
        guard n == start.count , m == query.count else {
            print("error")
            return nil
        }
        var purpleArr = [(Int, Int)]()
        for (index, startNum) in start.enumerated() {
            let purple : (Int, Int) = (startNum, end[index])
            purpleArr.append(purple)
        }
        let purpleSort = purpleArr.sorted { (s1, s2) -> Bool in
            return s1.0 < s2.0
        }
        print("排序好的 purple : \(purpleSort)")
        var outPutArr = [Int]()
        var outPutDic = [Int:Int]() //缓存dictionary
        for (_, task) in query.enumerated() {
            var outNumber = 0
            if let cacheNumber = outPutDic[task] {
                outNumber = cacheNumber
                print("task = \(task), 缓存值为 \(cacheNumber)")
            }else{
                for (_, purpleItem) in purpleSort.enumerated() {
                    guard task >= purpleItem.0 else {
                        break
                    }
                    if task < purpleItem.1 {
                        outNumber += 1
                    }
                }
            }
            outPutDic[task] = outNumber
            outPutArr.append(outNumber)
        }
        print("输出数组:\(outPutArr)")
        return outPutArr
    }
上一篇下一篇

猜你喜欢

热点阅读