mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 09:23:19 +08:00
Update solution 707、918
This commit is contained in:
@ -1,82 +1,97 @@
|
||||
package leetcode
|
||||
|
||||
type MyLinkedList struct {
|
||||
head *Node
|
||||
}
|
||||
|
||||
type Node struct {
|
||||
Val int
|
||||
Next *MyLinkedList
|
||||
Next *Node
|
||||
Prev *Node
|
||||
}
|
||||
|
||||
/** Initialize your data structure here. */
|
||||
func Constructor() MyLinkedList {
|
||||
return MyLinkedList{Val: -999, Next: nil}
|
||||
return MyLinkedList{}
|
||||
}
|
||||
|
||||
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
|
||||
func (this *MyLinkedList) Get(index int) int {
|
||||
cur := this
|
||||
for i := 0; cur != nil; i++ {
|
||||
if i == index {
|
||||
if cur.Val == -999 {
|
||||
return -1
|
||||
} else {
|
||||
return cur.Val
|
||||
}
|
||||
}
|
||||
cur = cur.Next
|
||||
curr := this.head
|
||||
for i := 0; i < index && curr != nil; i++ {
|
||||
curr = curr.Next
|
||||
}
|
||||
if curr != nil {
|
||||
return curr.Val
|
||||
} else {
|
||||
return -1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
|
||||
func (this *MyLinkedList) AddAtHead(val int) {
|
||||
if this.Val == -999 {
|
||||
this.Val = val
|
||||
} else {
|
||||
tmp := &MyLinkedList{Val: this.Val, Next: this.Next}
|
||||
this.Val = val
|
||||
this.Next = tmp
|
||||
node := &Node{Val: val}
|
||||
node.Next = this.head
|
||||
if this.head != nil {
|
||||
this.head.Prev = node
|
||||
}
|
||||
this.head = node
|
||||
}
|
||||
|
||||
/** Append a node of value val to the last element of the linked list. */
|
||||
func (this *MyLinkedList) AddAtTail(val int) {
|
||||
cur := this
|
||||
for cur.Next != nil {
|
||||
cur = cur.Next
|
||||
if this.head == nil {
|
||||
this.AddAtHead(val)
|
||||
return
|
||||
}
|
||||
tmp := &MyLinkedList{Val: val, Next: nil}
|
||||
cur.Next = tmp
|
||||
node := &Node{Val: val}
|
||||
curr := this.head
|
||||
for curr != nil && curr.Next != nil {
|
||||
curr = curr.Next
|
||||
}
|
||||
node.Prev = curr
|
||||
curr.Next = node
|
||||
}
|
||||
|
||||
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
|
||||
func (this *MyLinkedList) AddAtIndex(index int, val int) {
|
||||
cur := this
|
||||
if index == 0 {
|
||||
this.AddAtHead(val)
|
||||
return
|
||||
}
|
||||
for i := 0; cur != nil; i++ {
|
||||
if i == index-1 {
|
||||
break
|
||||
} else {
|
||||
node := &Node{Val: val}
|
||||
curr := this.head
|
||||
for i := 0; i < index-1 && curr != nil; i++ {
|
||||
curr = curr.Next
|
||||
}
|
||||
if curr != nil {
|
||||
node.Next = curr.Next
|
||||
node.Prev = curr
|
||||
if node.Next != nil {
|
||||
node.Next.Prev = node
|
||||
}
|
||||
curr.Next = node
|
||||
}
|
||||
cur = cur.Next
|
||||
}
|
||||
if cur != nil && cur.Val != -999 {
|
||||
tmp := &MyLinkedList{Val: val, Next: cur.Next}
|
||||
cur.Next = tmp
|
||||
}
|
||||
}
|
||||
|
||||
/** Delete the index-th node in the linked list, if the index is valid. */
|
||||
func (this *MyLinkedList) DeleteAtIndex(index int) {
|
||||
cur := this
|
||||
for i := 0; cur != nil; i++ {
|
||||
if i == index-1 {
|
||||
break
|
||||
if index == 0 {
|
||||
this.head = this.head.Next
|
||||
if this.head != nil {
|
||||
this.head.Prev = nil
|
||||
}
|
||||
} else {
|
||||
curr := this.head
|
||||
for i := 0; i < index-1 && curr != nil; i++ {
|
||||
curr = curr.Next
|
||||
}
|
||||
if curr != nil && curr.Next != nil {
|
||||
curr.Next = curr.Next.Next
|
||||
if curr.Next != nil {
|
||||
curr.Next.Prev = curr
|
||||
}
|
||||
}
|
||||
cur = cur.Next
|
||||
}
|
||||
if cur != nil && cur.Next != nil {
|
||||
cur.Next = cur.Next.Next
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,11 +54,10 @@ func Test_Problem707(t *testing.T) {
|
||||
|
||||
func MList2Ints(head *MyLinkedList) []int {
|
||||
res := []int{}
|
||||
|
||||
for head != nil {
|
||||
res = append(res, head.Val)
|
||||
head = head.Next
|
||||
cur := head.head
|
||||
for cur != nil {
|
||||
res = append(res, cur.Val)
|
||||
cur = cur.Next
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
@ -2,34 +2,50 @@ package leetcode
|
||||
|
||||
import "math"
|
||||
|
||||
func maxSubarraySumCircular(A []int) int {
|
||||
n, sum := len(A), 0
|
||||
for _, v := range A {
|
||||
sum += v
|
||||
func maxSubarraySumCircular(nums []int) int {
|
||||
var max1, max2, sum int
|
||||
|
||||
// case: no circulation
|
||||
max1 = int(math.Inf(-1))
|
||||
l := len(nums)
|
||||
for i := 0; i < l; i++ {
|
||||
sum += nums[i]
|
||||
if sum > max1 {
|
||||
max1 = sum
|
||||
}
|
||||
if sum < 1 {
|
||||
sum = 0
|
||||
}
|
||||
}
|
||||
kad := kadane(A)
|
||||
for i := 0; i < n; i++ {
|
||||
A[i] = -A[i]
|
||||
|
||||
// case: circling
|
||||
arr_sum := 0
|
||||
for i := 0; i < l; i++ {
|
||||
arr_sum += nums[i]
|
||||
}
|
||||
negativeMax := kadane(A)
|
||||
if sum+negativeMax <= 0 {
|
||||
return kad
|
||||
|
||||
sum = 0
|
||||
min_sum := 0
|
||||
for i := 1; i < l-1; i++ {
|
||||
sum += nums[i]
|
||||
if sum >= 0 {
|
||||
sum = 0
|
||||
}
|
||||
if sum < min_sum {
|
||||
min_sum = sum
|
||||
}
|
||||
}
|
||||
return max(kad, sum+negativeMax)
|
||||
max2 = arr_sum - min_sum
|
||||
|
||||
return max(max1, max2)
|
||||
}
|
||||
|
||||
func kadane(a []int) int {
|
||||
n, MaxEndingHere, maxSoFar := len(a), a[0], math.MinInt32
|
||||
for i := 1; i < n; i++ {
|
||||
MaxEndingHere = max(a[i], MaxEndingHere+a[i])
|
||||
maxSoFar = max(MaxEndingHere, maxSoFar)
|
||||
func max(nums ...int) int {
|
||||
max := int(math.Inf(-1))
|
||||
for _, num := range nums {
|
||||
if num > max {
|
||||
max = num
|
||||
}
|
||||
}
|
||||
return maxSoFar
|
||||
}
|
||||
|
||||
func max(a int, b int) int {
|
||||
if a > b {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
return max
|
||||
}
|
||||
|
Reference in New Issue
Block a user