Skip to content

Commit 9e50a6b

Browse files
committed
add new files
1 parent 5556806 commit 9e50a6b

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+
var res [][]string
8+
9+
func partition(s string) [][]string {
10+
res = [][]string{}
11+
if len(s) < 1 {
12+
return res
13+
}
14+
helper(s, 0, []string{})
15+
return res
16+
}
17+
func helper(s string, start int, tmp []string) {
18+
if start == len(s) {
19+
m := make([]string, len(tmp))
20+
copy(m, tmp)
21+
res = append(res, m)
22+
return
23+
}
24+
for i := start; i < len(s); i++ {
25+
if isPalindrome(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+
func isPalindrome(s string, start, end int) bool {
34+
for start < end {
35+
if s[start] != s[end] {
36+
return false
37+
}
38+
start++
39+
end--
40+
}
41+
return true
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+
type ListNode struct {
4+
Val int
5+
Next *ListNode
6+
}
7+
8+
/**
9+
用快慢指针的方式,快指针每次走两步,慢指针每次走一步,相遇的时候,就是环的位置
10+
此时slow停的位置到环入口的距离,等于头结点到环入口的距离(https://blog.csdn.net/jhlovetll/article/details/85255708)
11+
所以两个结点前进相同的步数,head就位于环的入口处了
12+
*/
13+
func detectCycle(head *ListNode) *ListNode {
14+
if head == nil || head.Next == nil {
15+
return nil
16+
}
17+
slow, fast := head.Next, head.Next.Next
18+
for fast != nil && fast.Next != nil && slow != fast {
19+
slow, fast = slow.Next, fast.Next.Next
20+
}
21+
//没有环
22+
if slow != fast {
23+
return nil
24+
}
25+
for slow != head {
26+
slow, head = slow.Next, head.Next
27+
}
28+
return slow
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+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
8+
9+
/**
10+
用一个slice实现stack结构,首先将root和root的左子树入栈
11+
出栈的同时,将右子树入栈
12+
*/
13+
14+
type BSTIterator struct {
15+
stack []*TreeNode
16+
}
17+
18+
func Constructor(root *TreeNode) BSTIterator {
19+
stack := make([]*TreeNode, 0, 128)
20+
res := BSTIterator{
21+
stack: stack,
22+
}
23+
res.push(root)
24+
return res
25+
}
26+
27+
/** @return the next smallest number */
28+
func (this *BSTIterator) Next() int {
29+
size := len(this.stack)
30+
var top *TreeNode
31+
this.stack, top = this.stack[:size-1], this.stack[size-1]
32+
this.push(top.Right)
33+
return top.Val
34+
35+
}
36+
37+
/** @return whether we have a next smallest number */
38+
func (this *BSTIterator) HasNext() bool {
39+
return len(this.stack) > 0
40+
}
41+
42+
func (this *BSTIterator) push(root *TreeNode) {
43+
for root != 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+
func singleNumber(nums []int) []int {
7+
res := make([]int, 2)
8+
xor := nums[0]
9+
for i := 1; i < len(nums); i++ {
10+
xor ^= nums[i]
11+
}
12+
bit := xor &^ (xor - 1)
13+
n1, n2 := 0, 0
14+
for _, v := range nums {
15+
if v&bit > 0 {
16+
n1 ^= v
17+
} else {
18+
n2 ^= v
19+
}
20+
}
21+
res[0] = n1
22+
res[1] = n2
23+
return res
24+
}

0 commit comments

Comments
 (0)