funcintervalIntersection(firstList [][]int, secondList [][]int) [][]int { result := make([][]int, 0) m, n := len(firstList), len(secondList) i, j := 0, 0
for i < m && j < n { firstStart, firstEnd := firstList[i][0], firstList[i][1] secondStart, secondEnd := secondList[j][0], secondList[j][1]
// 两个区间存在交集 if secondEnd >= firstStart && firstEnd >= secondStart { result = append(result, []int{max(firstStart, secondStart), min(firstEnd, secondEnd)}) } if secondEnd < firstEnd { j++ } else { i++ } } return result }
funcmax(a, b int)int { if a > b { return a } return b }
funcmin(a, b int)int { if a < b { return a } return b }