美文网首页
task_number_count-swift

task_number_count-swift

作者: 空降哥伦比亚 | 来源:发表于2017-08-01 14:45 被阅读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
        }
    

    相关文章

      网友评论

          本文标题:task_number_count-swift

          本文链接:https://www.haomeiwen.com/subject/joujlxtx.html