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

Commita293e17

Browse files
committed
add new files
1 parentad9fb1e commita293e17

5 files changed

+156
-0
lines changed

‎481. Magical String.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
/**
4+
生成magic string的同时 ,计算1的数量
5+
*/
6+
7+
funcmagicalString(nint)int {
8+
ifn<1 {
9+
return0
10+
}
11+
ifn<4 {
12+
return1
13+
}
14+
magic:=make([]int,n)
15+
magic[0]=1
16+
magic[1]=2
17+
magic[2]=2
18+
ret,next:=1,1
19+
countIndex:=2
20+
count:=magic[countIndex]
21+
i:=3
22+
fori<n {
23+
forcount>0&&i<n {
24+
ifnext==1 {
25+
ret++
26+
}
27+
magic[i]=next
28+
count--
29+
i++
30+
}
31+
ifnext==1 {
32+
next=2
33+
}else {
34+
next=1
35+
}
36+
countIndex++
37+
count=magic[countIndex]
38+
}
39+
returnret
40+
}

‎486. Predict the Winner.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
/**
4+
DP
5+
dp[i][j]表示原数组中从i到j的这么多数中,按照游戏规则,某个玩家所能获得的最大分数。
6+
假设这个分数此时属于palyer1,那么dp[i+1][j]或者dp[i][j-1]表示player2玩家所能获得的最大分数。因为对于player1来讲,他第一次选择要么是第i个数,要么是第j个数,所以对于player2来讲,就分两种情况取最大。
7+
另外我们设从i到j的所有数的和是sum[i,j],则可以得到递推公式:(动态规划最明显的标识)
8+
dp[i][j]=max(sum[i+1][j]-dp[i+1][j]+nums[i], sum[i][j-1]-dp[i][j-1]+nums[j]) 。
9+
化简一下:
10+
dp[i][j]=max(sum[i][j]-dp[i+1][j], sum[i][j]-dp[i][j-1]) 。
11+
*/
12+
funcPredictTheWinner(nums []int)bool {
13+
l:=len(nums)
14+
dp:=make([][]int,l+1)
15+
fork:=rangedp {
16+
dp[k]=make([]int,l)
17+
}
18+
fori:=l;i>=0;i-- {
19+
forj:=i+1;j<l;j++ {
20+
a:=nums[i]-dp[i+1][j]
21+
b:=nums[j]-dp[i][j-1]
22+
dp[i][j]=max(a,b)
23+
}
24+
}
25+
returndp[0][l-1]>=0
26+
}
27+
28+
funcmax(x,yint)int {
29+
ifx>y {
30+
returnx
31+
}
32+
returny
33+
}

‎492. Construct the Rectangle.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import"math"
4+
5+
funcconstructRectangle(areaint) []int {
6+
i:=int(math.Sqrt(float64(area)))
7+
forarea%i!=0 {
8+
i--
9+
}
10+
ret:=make([]int,2)
11+
ret[0]=max(i,area/i)
12+
ret[1]=min(i,area/i)
13+
returnret
14+
}
15+
funcmax(x,yint)int {
16+
ifx>y {
17+
returnx
18+
}
19+
returny
20+
}
21+
funcmin(x,yint)int {
22+
ifx>y {
23+
returny
24+
}
25+
returnx
26+
}

‎518. Coin Change 2.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
/**
4+
DP
5+
*/
6+
funcchange(amountint,coins []int)int {
7+
dp:=make([]int,amount+1)
8+
dp[0]=1
9+
forj:=0;j<len(coins);j++ {
10+
fori:=1;i<=amount;i++ {
11+
ifi-coins[j]>=0 {
12+
dp[i]+=dp[i-coins[j]]
13+
}
14+
}
15+
}
16+
returndp[amount]
17+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
//TODO TLE
4+
funcfindLUSlength(strs []string)int {
5+
res,n:=-1,len(strs)
6+
fori:=0;i<n;i++ {
7+
iflen(strs[i])<res {
8+
continue
9+
}
10+
j:=-1
11+
forj<n {
12+
j++
13+
ifi!=j&&isSub(strs[i],strs[j]) {
14+
break
15+
}
16+
}
17+
ifj==n {
18+
res=max(res,len(strs[i]))
19+
}
20+
}
21+
returnres
22+
}
23+
24+
funcmax(x,yint)int {
25+
ifx>y {
26+
returnx
27+
}
28+
returny
29+
}
30+
31+
funcisSub(a,bstring)bool {
32+
i,j:=0,0
33+
fori<len(a)&&j<len(b) {
34+
ifa[i]==b[j] {
35+
i++
36+
j++
37+
}
38+
}
39+
returni==len(a)
40+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp