classSolution(object): definsert(self, intervals, newInterval): """ :type intervals: List[List[int]] :type newInterval: List[int] :rtype: List[List[int]] """ result = [] i = 0 while i < len(intervals) and intervals[i][1] < newInterval[0]: result.append(intervals[i]) i += 1 while i < len(intervals) and intervals[i][0] <= newInterval[1]: newInterval[0] = min(newInterval[0], intervals[i][0]) newInterval[1] = max(newInterval[1], intervals[i][1]) i += 1 result.append(newInterval) while i < len(intervals): result.append(intervals[i]) i += 1 return result
结果
执行用时 : 20 ms, 击败 75.85% 使用 Python 的用户
内存消耗 : 13.05 MB, 击败 81.10% 使用 Python 的用户
Python3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution: definsert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]: result = [] i = 0 while i < len(intervals) and intervals[i][1] < newInterval[0]: result.append(intervals[i]) i += 1 while i < len(intervals) and intervals[i][0] <= newInterval[1]: newInterval[0] = min(newInterval[0], intervals[i][0]) newInterval[1] = max(newInterval[1], intervals[i][1]) i += 1 result.append(newInterval) while i < len(intervals): result.append(intervals[i]) i += 1 return result
classSolution { funcinsert(_intervals: [[Int]], _newInterval: [Int]) -> [[Int]] { var result: [[Int]] = [] var left = newInterval[0] var right = newInterval[1] var placed =false for interval in intervals { if interval[1] < left { result.append(interval) } elseif interval[0] > right { if!placed { result.append([left, right]) placed =true } result.append(interval) } else { left =min(left, interval[0]) right =max(right, interval[1]) } } if!placed { result.append([left, right]) } return result } }
funcinsert(intervals [][]int, newInterval []int) [][]int { result := make([][]int, 0) left, right := newInterval[0], newInterval[1] placed := false for _, interval := range intervals { if interval[1] < left { result = append(result, interval) } elseif interval[0] > right { if !placed { result = append(result, []int{left, right}) placed = true } result = append(result, interval) } else { left = min(left, interval[0]) right = max(right, interval[1]) } } if !placed { result = append(result, []int{left, right}) } return result } funcmin(a, b int)int { if a < b { return a } return b } funcmax(a, b int)int { if a > b { return a } return b }
# @param {Integer[][]} intervals # @param {Integer[]} new_interval # @return {Integer[][]} definsert(intervals, new_interval) result = [] left, right = new_interval[0], new_interval[1] placed = false intervals.each do |interval| if interval[1] < left result << interval elsif interval[0] > right unless placed result << [left, right] placed = true end result << interval else left = [left, interval[0]].min right = [right, interval[1]].max end end result << [left, right] unless placed result end
objectSolution{ definsert(intervals: Array[Array[Int]], newInterval: Array[Int]): Array[Array[Int]] = { var result: List[Array[Int]] = List() var left = newInterval(0) var right = newInterval(1) var placed = false for (interval <- intervals) { if (interval(1) < left) { result :+= interval } elseif (interval(0) > right) { if (!placed) { result :+= Array(left, right) placed = true } result :+= interval } else { left = left min interval(0) right = right max interval(1) } } if (!placed) { result :+= Array(left, right) } result.toArray } }