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

Commit57590df

Browse files
committed
add new files
1 parent9e50a6b commit57590df

5 files changed

+165
-0
lines changed

‎474. Ones and Zeroes.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
funcfindMaxForm(strs []string,mint,nint)int {
4+
dp:=make([][]int,m+1)
5+
fork,_:=rangedp {
6+
dp[k]=make([]int,n+1)
7+
}
8+
for_,v:=rangestrs {
9+
c0,c1:=0,0
10+
fori:=0;i<len(v);i++ {
11+
ifv[i]=='1' {
12+
c1++
13+
}else {
14+
c0++
15+
}
16+
}
17+
ifc0>m||c1>n {
18+
continue
19+
}
20+
fori:=m;i>=c0;i-- {
21+
forj:=n;j>=c1;j-- {
22+
dp[i][j]=max(dp[i][j],dp[i-c0][j-c1]+1)
23+
}
24+
}
25+
}
26+
returndp[m][n]
27+
}
28+
29+
funcmax(x,yint)int {
30+
ifx>y {
31+
returnx
32+
}
33+
returny
34+
}

‎491. Increasing Subsequences.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
varres [][]int
4+
5+
funcisUsed(nums []int,start,indexint)bool {
6+
fori:=start;i<index;i++ {
7+
ifnums[i]==nums[index] {
8+
returntrue
9+
}
10+
}
11+
returnfalse
12+
}
13+
14+
funcfindSubsequences(nums []int) [][]int {
15+
res= [][]int{}
16+
dfs(nums,0, []int{})
17+
returnres
18+
19+
}
20+
21+
funcdfs(nums []int,startint,tmp []int) {
22+
iflen(tmp)>1 {
23+
res=append(res,tmp)
24+
}
25+
ifstart==len(nums) {
26+
return
27+
}
28+
fori:=start;i<len(nums);i++ {
29+
iflen(tmp)>0&&nums[i]<tmp[len(tmp)-1] {
30+
continue
31+
}
32+
ifi>start&&isUsed(nums,start,i) {
33+
continue
34+
}
35+
tmp=append(tmp,nums[i])
36+
dfs(nums,i+1,tmp)
37+
tmp=tmp[:len(tmp)-1]
38+
}
39+
}

‎496. Next Greater Element I.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
/**
4+
nums1 是nums2的子集,因此find方法可以找到nums1中的每个元素在nums2中的位置
5+
*/
6+
funcnextGreaterElement(nums1 []int,nums2 []int) []int {
7+
ret:=make([]int,len(nums1))
8+
fori:=0;i<len(nums1);i++ {
9+
index:=find(nums2,nums1[i])
10+
forj:=index;j<len(nums2);j++ {
11+
ifnums2[j]>nums1[i] {
12+
ret[i]=nums2[j]
13+
break
14+
}
15+
}
16+
ifret[i]==0 {
17+
ret[i]=-1
18+
}
19+
}
20+
returnret
21+
}
22+
23+
funcfind(A []int,aint)int {
24+
fori:=0;i<len(A);i++ {
25+
ifA[i]==a {
26+
returni
27+
}
28+
}
29+
return-1
30+
}

‎498. Diagonal Traverse.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
funcfindDiagonalOrder(matrix [][]int) []int {
4+
iflen(matrix)==0 {
5+
return []int{}
6+
}
7+
r,c:=0,0
8+
m,n:=len(matrix),len(matrix[0])
9+
ret:=make([]int,m*n)
10+
fori:=0;i<m*n;i++ {
11+
ret[i]=matrix[r][c]
12+
if (r+c)%2==0 {
13+
ifc==n-1 {
14+
r++
15+
}elseifr==0 {
16+
c++
17+
}else {
18+
r--
19+
c++
20+
}
21+
}else {
22+
ifr==m-1 {
23+
c++
24+
}elseifc==0 {
25+
r++
26+
}else {
27+
r++
28+
c--
29+
}
30+
}
31+
}
32+
returnret
33+
}

‎503. Next Greater Element II.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
/**
4+
把nums复制两倍,保证从任何一个位置开始,后面包含完整的数据
5+
*/
6+
7+
funcnextGreaterElements(nums []int) []int {
8+
l:=len(nums)
9+
l2:=2*l
10+
ret:=make([]int,l)
11+
index:=make([]int,l2)
12+
fori:=l;i<l2;i++ {
13+
index[i]=i-l
14+
}
15+
cur:=l
16+
fori:=l-1;i>=0;i-- {
17+
forcur<l2&&nums[index[cur]]<=nums[i] {
18+
cur++
19+
}
20+
ifcur==l2 {
21+
ret[i]=-1
22+
}else {
23+
ret[i]=nums[index[cur]]
24+
}
25+
cur--
26+
index[cur]=i
27+
}
28+
returnret
29+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp