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

Commit9bef0a0

Browse files
committed
add new files
1 parentf0eb0ff commit9bef0a0

5 files changed

+160
-0
lines changed

‎22. Generate Parentheses.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
varret []string
4+
5+
funcgenerateParenthesis(nint) []string {
6+
ret= []string{}
7+
helper("",n,n)
8+
returnret
9+
}
10+
11+
funchelper(itemstring,left,rightint) {
12+
ifleft<0||right<0||left>right {
13+
return
14+
}
15+
ifleft==0&&right==0 {
16+
ret=append(ret,item)
17+
return
18+
}
19+
helper(item+"(",left-1,right)
20+
helper(item+")",left,right-1)
21+
22+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
/**
4+
先用二分查找找到target,再向前后找到边界
5+
*/
6+
funchelp(nums []int,left,right,tint)int {
7+
ifleft>=right {
8+
ifnums[left]!=t {
9+
return-1
10+
}else {
11+
returnleft
12+
}
13+
}
14+
mid:=int((left+right)/2)
15+
ret:=help(nums,left,mid,t)
16+
ifret!=-1 {
17+
returnret
18+
}
19+
returnhelp(nums,mid+1,right,t)
20+
}
21+
funcsearchRange(nums []int,targetint) []int {
22+
iflen(nums)==0 {
23+
return []int{-1,-1}
24+
}
25+
t:=help(nums,0,len(nums)-1,target)
26+
ift==-1 {
27+
return []int{-1,-1}
28+
}
29+
left,right:=t,t
30+
forleft=t;left>=0&&nums[left]==target;left-- {
31+
}
32+
ifleft<0||nums[left]!=target {
33+
left++
34+
}
35+
forright=t;right<len(nums)&&nums[right]==target;right++ {
36+
37+
}
38+
ifright==len(nums)||nums[right]!=target {
39+
right--
40+
}
41+
return []int{left,right}
42+
}

‎49. Group Anagrams.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
/**
4+
分到同一个组的字符串的特点就是,排序之后是相同的
5+
实现了内置的排序接口
6+
先排序之后,用map保存出现过的字符串
7+
*/
8+
9+
import"sort"
10+
11+
typesortRunes []rune
12+
13+
func (ssortRunes)Less(i,jint)bool {
14+
returns[i]<s[j]
15+
}
16+
17+
func (ssortRunes)Swap(i,jint) {
18+
s[i],s[j]=s[j],s[i]
19+
}
20+
21+
func (ssortRunes)Len()int {
22+
returnlen(s)
23+
}
24+
25+
funcSortString(sstring)string {
26+
r:= []rune(s)
27+
sort.Sort(sortRunes(r))
28+
returnstring(r)
29+
}
30+
31+
funcgroupAnagrams(strs []string) [][]string {
32+
ret:= [][]string{}
33+
dic:=make(map[string]int,len(strs))
34+
idx:=0
35+
for_,str:=rangestrs {
36+
tmp:=SortString(str)
37+
val,ok:=dic[tmp]
38+
ifok==false {
39+
dic[tmp]=idx
40+
41+
ret=append(ret, []string{})
42+
ret[idx]=append(ret[idx],str)
43+
idx++
44+
}else {
45+
ret[val]=append(ret[val],str)
46+
}
47+
}
48+
returnret
49+
}

‎55. Jump Game.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
/**
4+
dp算法
5+
6+
*/
7+
funccanJump(nums []int)bool {
8+
iflen(nums)<=1 {
9+
returntrue
10+
}
11+
dp:=make([]bool,len(nums))
12+
dp[0]=true
13+
fori:=0;i<len(nums);i++ {
14+
ifdp[i]==false {
15+
continue
16+
}
17+
forj:=1;j<=nums[i]&&j+i<len(nums);j++ {
18+
dp[i+j]=true
19+
}
20+
}
21+
returndp[len(nums)-1]
22+
}

‎62. Unique Paths.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
/**
4+
dp算法
5+
*/
6+
7+
funcuniquePaths(mint,nint)int {
8+
dp:= [][]int{}
9+
fori:=0;i<m;i++ {
10+
tmp:=make([]int,n)
11+
dp=append(dp,tmp)
12+
}
13+
fori:=0;i<m;i++ {
14+
dp[i][0]=1
15+
}
16+
fori:=0;i<n;i++ {
17+
dp[0][i]=1
18+
}
19+
fori:=1;i<m;i++ {
20+
forj:=1;j<n;j++ {
21+
dp[i][j]=dp[i-1][j]+dp[i][j-1]
22+
}
23+
}
24+
returndp[m-1][n-1]
25+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp