Skip to content

Commit bc52992

Browse files
committed
Update solution 707、918
1 parent bc7f1d1 commit bc52992

32 files changed

+542
-255
lines changed

leetcode/0707.Design-Linked-List/707. Design Linked List.go

Lines changed: 58 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,97 @@
11
package leetcode
22

33
type MyLinkedList struct {
4+
head *Node
5+
}
6+
7+
type Node struct {
48
Val int
5-
Next *MyLinkedList
9+
Next *Node
10+
Prev *Node
611
}
712

813
/** Initialize your data structure here. */
914
func Constructor() MyLinkedList {
10-
return MyLinkedList{Val: -999, Next: nil}
15+
return MyLinkedList{}
1116
}
1217

1318
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
1419
func (this *MyLinkedList) Get(index int) int {
15-
cur := this
16-
for i := 0; cur != nil; i++ {
17-
if i == index {
18-
if cur.Val == -999 {
19-
return -1
20-
} else {
21-
return cur.Val
22-
}
23-
}
24-
cur = cur.Next
20+
curr := this.head
21+
for i := 0; i < index && curr != nil; i++ {
22+
curr = curr.Next
23+
}
24+
if curr != nil {
25+
return curr.Val
26+
} else {
27+
return -1
2528
}
26-
return -1
2729
}
2830

2931
/** 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. */
3032
func (this *MyLinkedList) AddAtHead(val int) {
31-
if this.Val == -999 {
32-
this.Val = val
33-
} else {
34-
tmp := &MyLinkedList{Val: this.Val, Next: this.Next}
35-
this.Val = val
36-
this.Next = tmp
33+
node := &Node{Val: val}
34+
node.Next = this.head
35+
if this.head != nil {
36+
this.head.Prev = node
3737
}
38+
this.head = node
3839
}
3940

4041
/** Append a node of value val to the last element of the linked list. */
4142
func (this *MyLinkedList) AddAtTail(val int) {
42-
cur := this
43-
for cur.Next != nil {
44-
cur = cur.Next
43+
if this.head == nil {
44+
this.AddAtHead(val)
45+
return
4546
}
46-
tmp := &MyLinkedList{Val: val, Next: nil}
47-
cur.Next = tmp
47+
node := &Node{Val: val}
48+
curr := this.head
49+
for curr != nil && curr.Next != nil {
50+
curr = curr.Next
51+
}
52+
node.Prev = curr
53+
curr.Next = node
4854
}
4955

5056
/** 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. */
5157
func (this *MyLinkedList) AddAtIndex(index int, val int) {
52-
cur := this
5358
if index == 0 {
5459
this.AddAtHead(val)
55-
return
56-
}
57-
for i := 0; cur != nil; i++ {
58-
if i == index-1 {
59-
break
60+
} else {
61+
node := &Node{Val: val}
62+
curr := this.head
63+
for i := 0; i < index-1 && curr != nil; i++ {
64+
curr = curr.Next
65+
}
66+
if curr != nil {
67+
node.Next = curr.Next
68+
node.Prev = curr
69+
if node.Next != nil {
70+
node.Next.Prev = node
71+
}
72+
curr.Next = node
6073
}
61-
cur = cur.Next
62-
}
63-
if cur != nil && cur.Val != -999 {
64-
tmp := &MyLinkedList{Val: val, Next: cur.Next}
65-
cur.Next = tmp
6674
}
6775
}
6876

6977
/** Delete the index-th node in the linked list, if the index is valid. */
7078
func (this *MyLinkedList) DeleteAtIndex(index int) {
71-
cur := this
72-
for i := 0; cur != nil; i++ {
73-
if i == index-1 {
74-
break
79+
if index == 0 {
80+
this.head = this.head.Next
81+
if this.head != nil {
82+
this.head.Prev = nil
83+
}
84+
} else {
85+
curr := this.head
86+
for i := 0; i < index-1 && curr != nil; i++ {
87+
curr = curr.Next
88+
}
89+
if curr != nil && curr.Next != nil {
90+
curr.Next = curr.Next.Next
91+
if curr.Next != nil {
92+
curr.Next.Prev = curr
93+
}
7594
}
76-
cur = cur.Next
77-
}
78-
if cur != nil && cur.Next != nil {
79-
cur.Next = cur.Next.Next
8095
}
8196
}
8297

leetcode/0707.Design-Linked-List/707. Design Linked List_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,10 @@ func Test_Problem707(t *testing.T) {
5454

5555
func MList2Ints(head *MyLinkedList) []int {
5656
res := []int{}
57-
58-
for head != nil {
59-
res = append(res, head.Val)
60-
head = head.Next
57+
cur := head.head
58+
for cur != nil {
59+
res = append(res, cur.Val)
60+
cur = cur.Next
6161
}
62-
6362
return res
6463
}

leetcode/0918.Maximum-Sum-Circular-Subarray/918. Maximum Sum Circular Subarray.go

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,50 @@ package leetcode
22

33
import "math"
44

5-
func maxSubarraySumCircular(A []int) int {
6-
n, sum := len(A), 0
7-
for _, v := range A {
8-
sum += v
9-
}
10-
kad := kadane(A)
11-
for i := 0; i < n; i++ {
12-
A[i] = -A[i]
5+
func maxSubarraySumCircular(nums []int) int {
6+
var max1, max2, sum int
7+
8+
// case: no circulation
9+
max1 = int(math.Inf(-1))
10+
l := len(nums)
11+
for i := 0; i < l; i++ {
12+
sum += nums[i]
13+
if sum > max1 {
14+
max1 = sum
15+
}
16+
if sum < 1 {
17+
sum = 0
18+
}
1319
}
14-
negativeMax := kadane(A)
15-
if sum+negativeMax <= 0 {
16-
return kad
20+
21+
// case: circling
22+
arr_sum := 0
23+
for i := 0; i < l; i++ {
24+
arr_sum += nums[i]
1725
}
18-
return max(kad, sum+negativeMax)
19-
}
2026

21-
func kadane(a []int) int {
22-
n, MaxEndingHere, maxSoFar := len(a), a[0], math.MinInt32
23-
for i := 1; i < n; i++ {
24-
MaxEndingHere = max(a[i], MaxEndingHere+a[i])
25-
maxSoFar = max(MaxEndingHere, maxSoFar)
27+
sum = 0
28+
min_sum := 0
29+
for i := 1; i < l-1; i++ {
30+
sum += nums[i]
31+
if sum >= 0 {
32+
sum = 0
33+
}
34+
if sum < min_sum {
35+
min_sum = sum
36+
}
2637
}
27-
return maxSoFar
38+
max2 = arr_sum - min_sum
39+
40+
return max(max1, max2)
2841
}
2942

30-
func max(a int, b int) int {
31-
if a > b {
32-
return a
43+
func max(nums ...int) int {
44+
max := int(math.Inf(-1))
45+
for _, num := range nums {
46+
if num > max {
47+
max = num
48+
}
3349
}
34-
return b
50+
return max
3551
}

website/content/ChapterFour/0500~0599/0538.Convert-BST-to-Greater-Tree.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,5 @@ func dfs538(root *TreeNode, sum *int) {
109109
----------------------------------------------
110110
<div style="display: flex;justify-content: space-between;align-items: center;">
111111
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0537.Complex-Number-Multiplication/">⬅️上一页</a></p>
112-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0541.Reverse-String-II/">下一页➡️</a></p>
112+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0540.Single-Element-in-a-Sorted-Array/">下一页➡️</a></p>
113113
</div>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# [540. Single Element in a Sorted Array](https://leetcode.com/problems/single-element-in-a-sorted-array/)
2+
3+
## 题目
4+
5+
You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.
6+
7+
Return the single element that appears only once.
8+
9+
Your solution must run in O(log n) time and O(1) space.
10+
11+
**Example 1:**
12+
13+
Input: nums = [1,1,2,3,3,4,4,8,8]
14+
Output: 2
15+
16+
**Example 2:**
17+
18+
Input: nums = [3,3,7,7,10,11,11]
19+
Output: 10
20+
21+
**Constraints:**
22+
23+
- 1 <= nums.length <= 100000
24+
- 0 <= nums[i] <= 100000
25+
26+
## 题目大意
27+
28+
给你一个仅由整数组成的有序数组,其中每个元素都会出现两次,唯有一个数只会出现一次。
29+
30+
请你找出并返回只出现一次的那个数。
31+
32+
你设计的解决方案必须满足 O(log n) 时间复杂度和 O(1) 空间复杂度。
33+
34+
## 解题思路
35+
36+
假设下标idx是单独的数字,idx左边的偶数下标x有nums[x] == nums[x + 1],
37+
idx右边的奇数下标y有nums[y] == nums[y + 1],可以根据此特性用二分查找idx对应的值
38+
39+
## 代码
40+
41+
```go
42+
package leetcode
43+
44+
func singleNonDuplicate(nums []int) int {
45+
left, right := 0, len(nums)-1
46+
for left < right {
47+
mid := (left + right) / 2
48+
if mid%2 == 0 {
49+
if nums[mid] == nums[mid+1] {
50+
left = mid + 1
51+
} else {
52+
right = mid
53+
}
54+
} else {
55+
if nums[mid] == nums[mid-1] {
56+
left = mid + 1
57+
} else {
58+
right = mid
59+
}
60+
}
61+
}
62+
return nums[left]
63+
}
64+
```
65+
66+
67+
----------------------------------------------
68+
<div style="display: flex;justify-content: space-between;align-items: center;">
69+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0538.Convert-BST-to-Greater-Tree/">⬅️上一页</a></p>
70+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0541.Reverse-String-II/">下一页➡️</a></p>
71+
</div>

website/content/ChapterFour/0500~0599/0541.Reverse-String-II.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@ func revers(s string) string {
7070

7171
----------------------------------------------
7272
<div style="display: flex;justify-content: space-between;align-items: center;">
73-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0538.Convert-BST-to-Greater-Tree/">⬅️上一页</a></p>
73+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0540.Single-Element-in-a-Sorted-Array/">⬅️上一页</a></p>
7474
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0500~0599/0542.01-Matrix/">下一页➡️</a></p>
7575
</div>

0 commit comments

Comments
 (0)