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

Commit17ed604

Browse files
committed
add new files
1 parent3e36216 commit17ed604

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

‎1122. Relative Sort Array.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1.
3+
4+
Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. Elements that don't appear in arr2 should be placed at the end of arr1 in ascending order.
5+
6+
7+
8+
Example 1:
9+
10+
Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]
11+
Output: [2,2,2,1,4,3,3,9,6,7,19]
12+
13+
*/
14+
package main
15+
16+
import"sort"
17+
18+
funcrelativeSortArray(arr1 []int,arr2 []int) []int {
19+
counter:=map[int]int{}
20+
others:= []int{}
21+
dest:=make([]int,0,len(arr2))
22+
for_,v:=rangearr2 {
23+
counter[v]=0
24+
}
25+
for_,v:=rangearr1 {
26+
if_,ok:=counter[v];ok {
27+
counter[v]++
28+
}else {
29+
others=append(others,v)
30+
}
31+
}
32+
33+
for_,v:=rangearr2 {
34+
fori:=0;i<counter[v];i++ {
35+
dest=append(dest,v)
36+
}
37+
}
38+
sort.Ints(others)
39+
dest=append(dest,others...)
40+
returndest
41+
}

‎1154. Day of the Year.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the day number of the year.
3+
4+
5+
6+
Example 1:
7+
8+
Input: date = "2019-01-09"
9+
Output: 9
10+
Explanation: Given date is the 9th day of the year in 2019.
11+
Example 2:
12+
13+
Input: date = "2019-02-10"
14+
Output: 41
15+
16+
*/
17+
package main
18+
19+
import"strconv"
20+
21+
funcdayOfYear(datestring)int {
22+
months:= []int{31,28,31,30,31,30,31,31,30,31,30,31}
23+
year,_:=strconv.Atoi(date[:4])
24+
month,_:=strconv.Atoi(date[5:7])
25+
day,_:=strconv.Atoi(date[8:])
26+
ifyear%400==0|| (year%4==0&&year%100!=0) {
27+
months[1]=29
28+
}
29+
returnsum(months,month-1)+day
30+
}
31+
32+
funcsum(arr []int,indexint)int {
33+
varresint
34+
fori:=0;i<index;i++ {
35+
res+=arr[i]
36+
}
37+
returnres
38+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp