添加新题目
This commit is contained in:
@@ -1,9 +1,27 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
|
||||
setZeroes([][]int{{0, 1, 2, 0}, {3, 4, 5, 2}, {1, 3, 1, 5}})
|
||||
}
|
||||
|
||||
func setZeroes(matrix [][]int) {
|
||||
|
||||
col := make([]bool, len(matrix[0]))
|
||||
row := make([]bool, len(matrix))
|
||||
for i := range matrix {
|
||||
for j := range matrix[i] {
|
||||
if matrix[i][j] == 0 {
|
||||
col[j] = true
|
||||
row[i] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
//fmt.Println("col", col)
|
||||
//fmt.Println("row", row)
|
||||
for i := range matrix {
|
||||
for j := range matrix[i] {
|
||||
if row[i] || col[j] {
|
||||
matrix[i][j] = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
82
矩阵/螺旋矩阵/main.go
Normal file
82
矩阵/螺旋矩阵/main.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
//fmt.Println(spiralOrder([][]int{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24}}))
|
||||
}
|
||||
|
||||
//func spiralOrder(matrix [][]int) []int {
|
||||
// step := len(matrix) * len(matrix[0])
|
||||
// forward := make([][]string, len(matrix))
|
||||
// result := make([]int, 0)
|
||||
// for i := range forward {
|
||||
// forward[i] = make([]string, len(matrix[0]))
|
||||
// for j := range forward[i] {
|
||||
// forward[i][j] = "" // 初始化为空字符串
|
||||
// }
|
||||
// }
|
||||
// //fmt.Println(forward, "1")
|
||||
// for i := 0; i < len(matrix); i++ {
|
||||
// for j := 0; j < len(matrix[i]); j++ {
|
||||
// if j-1 < 0 && i-1 < 0 {
|
||||
// if len(matrix[i]) == 1 {
|
||||
// forward[i][j] = "d"
|
||||
// continue
|
||||
// }
|
||||
// forward[i][j] = "r"
|
||||
// continue
|
||||
// }
|
||||
// if j+1 > len(forward[0])-1 && i-1 < 0 {
|
||||
// forward[i][j] = "d"
|
||||
// continue
|
||||
// }
|
||||
// if j+1 > len(forward[0])-1 && i+1 > len(forward)-1 {
|
||||
// forward[i][j] = "l"
|
||||
// continue
|
||||
// }
|
||||
// if j-1 < 0 && i+1 > len(forward)-1 {
|
||||
// forward[i][j] = "u"
|
||||
// continue
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// fmt.Println(forward, "2")
|
||||
// left, right := 0, 0
|
||||
// nowforward := forward[left][right]
|
||||
// for {
|
||||
// if step == 0 {
|
||||
// break
|
||||
// }
|
||||
// fmt.Println("当前forward:", nowforward)
|
||||
// if forward[left][right] != "" {
|
||||
// nowforward = forward[left][right]
|
||||
// }
|
||||
// if nowforward == "u" && left-1 >= 0 {
|
||||
// if forward[left-1][right] != "" {
|
||||
// //fmt.Println("向上:", forward[left-1][right])
|
||||
// result = append(result, matrix[left][right])
|
||||
// nowforward = "r"
|
||||
// right++
|
||||
// step--
|
||||
// continue
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// //fmt.Println("本次方向:", nowforward)
|
||||
// result = append(result, matrix[left][right])
|
||||
// if nowforward == "r" {
|
||||
// //fmt.Println("向右")
|
||||
// right++
|
||||
// } else if nowforward == "d" {
|
||||
// left++
|
||||
// } else if nowforward == "l" {
|
||||
// right--
|
||||
// } else if nowforward == "u" {
|
||||
// //fmt.Println("变更向上:", forward[left-1][right])
|
||||
// left--
|
||||
// }
|
||||
// step--
|
||||
// }
|
||||
// //fmt.Println(result)
|
||||
//
|
||||
// return result
|
||||
//}
|
||||
25
链表/反转链表/main.go
Normal file
25
链表/反转链表/main.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
type ListNode struct {
|
||||
Val int
|
||||
Next *ListNode
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
||||
func reverseList(head *ListNode) *ListNode {
|
||||
pre := (*ListNode)(nil)
|
||||
res := (*ListNode)(nil)
|
||||
for head != nil {
|
||||
next := head.Next
|
||||
head.Next = pre
|
||||
pre = head
|
||||
if next == nil {
|
||||
res = head
|
||||
break
|
||||
}
|
||||
head = next
|
||||
}
|
||||
return res
|
||||
}
|
||||
20
链表/合并两个有序链表/main.go
Normal file
20
链表/合并两个有序链表/main.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
type ListNode struct {
|
||||
Val int
|
||||
Next *ListNode
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
||||
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
|
||||
upP := list1
|
||||
dnP := list2
|
||||
for upP.Next != nil && dnP.Next != nil {
|
||||
if upP.Next == nil {
|
||||
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
40
链表/回文链表/main.go
Normal file
40
链表/回文链表/main.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
type ListNode struct {
|
||||
Val int
|
||||
Next *ListNode
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
||||
func isPalindrome(head *ListNode) bool {
|
||||
numlist := make([]int, 0)
|
||||
for head != nil {
|
||||
numlist = append(numlist, head.Val)
|
||||
head = head.Next
|
||||
}
|
||||
if len(numlist) == 1 {
|
||||
return true
|
||||
}
|
||||
left := len(numlist)/2 - 1
|
||||
right := left + 1
|
||||
if len(numlist)%2 != 0 {
|
||||
left = len(numlist) / 2
|
||||
right = left
|
||||
} else {
|
||||
left = len(numlist)/2 - 1
|
||||
right = left + 1
|
||||
}
|
||||
|
||||
for left >= 0 {
|
||||
if numlist[left] == numlist[right] {
|
||||
left--
|
||||
right++
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
23
链表/环形链表/main.go
Normal file
23
链表/环形链表/main.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
type ListNode struct {
|
||||
Val int
|
||||
Next *ListNode
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
||||
|
||||
func hasCycle(head *ListNode) bool {
|
||||
hash := make(map[*ListNode]struct{})
|
||||
for head != nil {
|
||||
if _, ok := hash[head.Next]; ok {
|
||||
return true
|
||||
}
|
||||
hash[head] = struct{}{}
|
||||
head = head.Next
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
22
链表/环形链表2/main.go
Normal file
22
链表/环形链表2/main.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
type ListNode struct {
|
||||
Val int
|
||||
Next *ListNode
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
||||
func detectCycle(head *ListNode) *ListNode {
|
||||
hash := make(map[*ListNode]struct{})
|
||||
for head != nil {
|
||||
if _, ok := hash[head.Next]; ok {
|
||||
return head.Next
|
||||
}
|
||||
hash[head] = struct{}{}
|
||||
head = head.Next
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
28
链表/相交链表/main.go
Normal file
28
链表/相交链表/main.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
type ListNode struct {
|
||||
Val int
|
||||
Next *ListNode
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
||||
func getIntersectionNode(headA, headB *ListNode) *ListNode {
|
||||
Anode, Bnode := headA, headB
|
||||
hash := make(map[*ListNode]struct{})
|
||||
|
||||
for Anode != nil {
|
||||
hash[Anode] = struct{}{}
|
||||
Anode = Anode.Next
|
||||
}
|
||||
for Bnode != nil {
|
||||
if _, ok := hash[Bnode]; ok {
|
||||
return Bnode
|
||||
}
|
||||
Bnode = Bnode.Next
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user