华为面试算法题

519 阅读1分钟

瞎 JB 解

func xiaozhang(_ A: [[String]]) -> Int {
 var startI = 0
     var D = [[[String]]]()
  var B = [[String]]()
  var C = [[String]]()
    while startI<A.count {
    for index in 0..<A.count {
        if A[startI][1] == A[index][0]{
            B.append(A[index])
        }else if A[startI][2] == A[index][0]{
            C.append(A[index])
        }
    }
        if B.count==0||C.count==0 {
            startI += 1
            continue
        }
    for bi in 0..<B.count {
        for ci in 0..<C.count{
            if B[bi][2] == C[ci][1]{
                D.append([A[startI],B[bi],C[ci]])
            }
        }
    }
      B.removeAll()
      C.removeAll()
      startI += 1
    }
    return  Set(D).count
}