Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc78ef30

Browse files
authored
Merge pull requestneetcode-gh#1940 from kkr2/feat/0057-insert-interval.go
Create 0057-insert-interval.go
2 parents9c6f4b2 +e3d4d55 commitc78ef30

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

‎go/0057-insert-interval.go‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
funcinsert(intervals [][]int,newInterval []int) [][]int {
2+
3+
res:= [][]int{}
4+
5+
fori,currInt:=rangeintervals {
6+
currStart,currEnd:=currInt[0],currInt[1]
7+
newIntStart,newIntEnd:=newInterval[0],newInterval[1]
8+
9+
ifnewIntEnd<currStart {
10+
res=append(res,newInterval)
11+
12+
res=append(res,intervals[i:]...)
13+
14+
returnres
15+
}elseifnewIntStart>currEnd {
16+
17+
res=append(res,currInt)
18+
19+
}else {
20+
newInterval[0]=min(newIntStart,currStart)
21+
newInterval[1]=max(newIntEnd,currEnd)
22+
23+
}
24+
}
25+
26+
res=append(res,newInterval)
27+
28+
returnres
29+
30+
}
31+
funcmin(a,bint)int {
32+
ifa>b {
33+
returnb
34+
}
35+
returna
36+
}
37+
38+
funcmax(a,bint)int {
39+
ifa>b {
40+
returna
41+
}
42+
returnb
43+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp