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

Commit9e50a6b

Browse files
committed
add new files
1 parent5556806 commit9e50a6b

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

‎131. Palindrome Partitioning.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
/**
4+
回溯算法搞定
5+
*/
6+
7+
varres [][]string
8+
9+
funcpartition(sstring) [][]string {
10+
res= [][]string{}
11+
iflen(s)<1 {
12+
returnres
13+
}
14+
helper(s,0, []string{})
15+
returnres
16+
}
17+
funchelper(sstring,startint,tmp []string) {
18+
ifstart==len(s) {
19+
m:=make([]string,len(tmp))
20+
copy(m,tmp)
21+
res=append(res,m)
22+
return
23+
}
24+
fori:=start;i<len(s);i++ {
25+
ifisPalindrome(s,start,i) {
26+
tmp=append(tmp,s[start:i+1])
27+
helper(s,i+1,tmp)
28+
tmp=tmp[:len(tmp)-1]
29+
}
30+
}
31+
}
32+
33+
funcisPalindrome(sstring,start,endint)bool {
34+
forstart<end {
35+
ifs[start]!=s[end] {
36+
returnfalse
37+
}
38+
start++
39+
end--
40+
}
41+
returntrue
42+
}

‎142. Linked List Cycle 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+
typeListNodestruct {
4+
Valint
5+
Next*ListNode
6+
}
7+
8+
/**
9+
用快慢指针的方式,快指针每次走两步,慢指针每次走一步,相遇的时候,就是环的位置
10+
此时slow停的位置到环入口的距离,等于头结点到环入口的距离(https://blog.csdn.net/jhlovetll/article/details/85255708)
11+
所以两个结点前进相同的步数,head就位于环的入口处了
12+
*/
13+
funcdetectCycle(head*ListNode)*ListNode {
14+
ifhead==nil||head.Next==nil {
15+
returnnil
16+
}
17+
slow,fast:=head.Next,head.Next.Next
18+
forfast!=nil&&fast.Next!=nil&&slow!=fast {
19+
slow,fast=slow.Next,fast.Next.Next
20+
}
21+
//没有环
22+
ifslow!=fast {
23+
returnnil
24+
}
25+
forslow!=head {
26+
slow,head=slow.Next,head.Next
27+
}
28+
returnslow
29+
}

‎173. Binary Search Tree Iterator.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
typeTreeNodestruct {
4+
Valint
5+
Left*TreeNode
6+
Right*TreeNode
7+
}
8+
9+
/**
10+
用一个slice实现stack结构,首先将root和root的左子树入栈
11+
出栈的同时,将右子树入栈
12+
*/
13+
14+
typeBSTIteratorstruct {
15+
stack []*TreeNode
16+
}
17+
18+
funcConstructor(root*TreeNode)BSTIterator {
19+
stack:=make([]*TreeNode,0,128)
20+
res:=BSTIterator{
21+
stack:stack,
22+
}
23+
res.push(root)
24+
returnres
25+
}
26+
27+
/** @return the next smallest number */
28+
func (this*BSTIterator)Next()int {
29+
size:=len(this.stack)
30+
vartop*TreeNode
31+
this.stack,top=this.stack[:size-1],this.stack[size-1]
32+
this.push(top.Right)
33+
returntop.Val
34+
35+
}
36+
37+
/** @return whether we have a next smallest number */
38+
func (this*BSTIterator)HasNext()bool {
39+
returnlen(this.stack)>0
40+
}
41+
42+
func (this*BSTIterator)push(root*TreeNode) {
43+
forroot!=nil {
44+
this.stack=append(this.stack,root)
45+
root=root.Left
46+
}
47+
}

‎260. Single Number III.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
/**
4+
如果能将A,B分开到二个数组中,那显然符合“异或”解法的关键点了。因此这个题目的关键点就是将A,B分开到二个数组中。由于A,B肯定是不相等的,因此在二进制上必定有一位是不同的。根据这一位是0还是1可以将A,B分开到A组和B组。而这个数组中其它数字要么就属于A组,要么就属于B组。再对A组和B组分别执行“异或”解法就可以得到A,B了。而要判断A,B在哪一位上不相同,只要根据A异或B的结果就可以知道了,这个结果在二进制上为1的位就说明A,B在这一位上是不相同的。
5+
*/
6+
funcsingleNumber(nums []int) []int {
7+
res:=make([]int,2)
8+
xor:=nums[0]
9+
fori:=1;i<len(nums);i++ {
10+
xor^=nums[i]
11+
}
12+
bit:=xor &^ (xor-1)
13+
n1,n2:=0,0
14+
for_,v:=rangenums {
15+
ifv&bit>0 {
16+
n1^=v
17+
}else {
18+
n2^=v
19+
}
20+
}
21+
res[0]=n1
22+
res[1]=n2
23+
returnres
24+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp