File tree 7 files changed +54
-8
lines changed
0081.Search in Rotated Sorted Array II
0083.Remove Duplicates from Sorted List
0100-0199/0105.Construct Binary Tree from Preorder and Inorder Traversal
7 files changed +54
-8
lines changed Original file line number Diff line number Diff line change 55
55
56
56
<!-- 这里可写通用的实现逻辑 -->
57
57
58
- 二分查找。
58
+ ** 方法一: 二分查找**
59
59
60
60
<!-- tabs:start -->
61
61
Original file line number Diff line number Diff line change 51
51
# self.val = val
52
52
# self.next = next
53
53
class Solution :
54
- def deleteDuplicates (self , head : ListNode) -> ListNode:
54
+ def deleteDuplicates (self , head : Optional[ ListNode] ) -> Optional[ ListNode] :
55
55
cur = head
56
56
while cur and cur.next:
57
57
if cur.val == cur.next.val:
@@ -61,6 +61,25 @@ class Solution:
61
61
return head
62
62
```
63
63
64
+ ``` python
65
+ # Definition for singly-linked list.
66
+ # class ListNode:
67
+ # def __init__(self, val=0, next=None):
68
+ # self.val = val
69
+ # self.next = next
70
+ class Solution :
71
+ def deleteDuplicates (self , head : Optional[ListNode]) -> Optional[ListNode]:
72
+ dummy = ListNode(1000 )
73
+ cur = dummy
74
+ while head:
75
+ if head.val != cur.val:
76
+ cur.next = head
77
+ cur = cur.next
78
+ head = head.next
79
+ cur.next = None
80
+ return dummy.next
81
+ ```
82
+
64
83
### ** Java**
65
84
66
85
<!-- 这里可写当前语言的特殊实现逻辑 -->
Original file line number Diff line number Diff line change 43
43
# self.val = val
44
44
# self.next = next
45
45
class Solution :
46
- def deleteDuplicates (self , head : ListNode) -> ListNode:
46
+ def deleteDuplicates (self , head : Optional[ ListNode] ) -> Optional[ ListNode] :
47
47
cur = head
48
48
while cur and cur.next:
49
49
if cur.val == cur.next.val:
@@ -53,6 +53,25 @@ class Solution:
53
53
return head
54
54
```
55
55
56
+ ``` python
57
+ # Definition for singly-linked list.
58
+ # class ListNode:
59
+ # def __init__(self, val=0, next=None):
60
+ # self.val = val
61
+ # self.next = next
62
+ class Solution :
63
+ def deleteDuplicates (self , head : Optional[ListNode]) -> Optional[ListNode]:
64
+ dummy = ListNode(1000 )
65
+ cur = dummy
66
+ while head:
67
+ if head.val != cur.val:
68
+ cur.next = head
69
+ cur = cur.next
70
+ head = head.next
71
+ cur.next = None
72
+ return dummy.next
73
+ ```
74
+
56
75
### ** Java**
57
76
58
77
``` java
Original file line number Diff line number Diff line change 40
40
41
41
<!-- 这里可写通用的实现逻辑 -->
42
42
43
+ ** 方法一:模拟**
44
+
43
45
创建两个链表,一个存放小于 ` x ` 的节点,另一个存放大于等于 ` x ` 的节点,之后进行拼接即可。
44
46
47
+ 时间复杂度 $O(n),空间复杂度 $O(1)$。其中 $n$ 是原链表的长度。
48
+
45
49
<!-- tabs:start -->
46
50
47
51
### ** Python3**
55
59
# self.val = val
56
60
# self.next = next
57
61
class Solution :
58
- def partition (self , head : ListNode, x : int ) -> ListNode:
62
+ def partition (self , head : Optional[ ListNode] , x : int ) -> Optional[ ListNode] :
59
63
d1, d2 = ListNode(), ListNode()
60
64
t1, t2 = d1, d2
61
65
while head:
Original file line number Diff line number Diff line change 45
45
# self.val = val
46
46
# self.next = next
47
47
class Solution :
48
- def partition (self , head : ListNode, x : int ) -> ListNode:
48
+ def partition (self , head : Optional[ ListNode] , x : int ) -> Optional[ ListNode] :
49
49
d1, d2 = ListNode(), ListNode()
50
50
t1, t2 = d1, d2
51
51
while head:
Original file line number Diff line number Diff line change 4
4
# self.val = val
5
5
# self.next = next
6
6
class Solution :
7
- def partition (self , head : ListNode , x : int ) -> ListNode :
7
+ def partition (self , head : Optional [ ListNode ] , x : int ) -> Optional [ ListNode ] :
8
8
d1 , d2 = ListNode (), ListNode ()
9
9
t1 , t2 = d1 , d2
10
10
while head :
Original file line number Diff line number Diff line change 42
42
43
43
<!-- 这里可写通用的实现逻辑 -->
44
44
45
- 前序序列的第一个结点 ` preorder[0] ` 为根节点,我们在中序序列中找到根节点的位置 i,可以将中序序列划分为左子树 ` inorder[:i] ` 、右子树 ` inorder[i+1:] ` 。
45
+ ** 方法一:递归 **
46
46
47
- 通过左右子树的区间,可以计算出左、右子树节点的个数,假设为 m、n。然后在前序节点中,从根节点往后的 m 个节点为左子树,再往后的 n 个节点为右子树。
47
+ 前序序列的第一个结点 $preorder[ 0] $ 为根节点,我们在中序序列中找到根节点的位置 $i$,可以将中序序列划分为左子树 $inorder[ 0..i] $ 、右子树 $inorder[ i+1..] $。
48
+
49
+ 通过左右子树的区间,可以计算出左、右子树节点的个数,假设为 $m$ 和 $n$。然后在前序节点中,从根节点往后的 $m$ 个节点为左子树,再往后的 $n$ 个节点为右子树。
48
50
49
51
递归求解即可。
50
52
51
53
> 前序遍历:先遍历根节点,再遍历左右子树;中序遍历:先遍历左子树,再遍历根节点,最后遍历右子树。
52
54
55
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。
56
+
53
57
<!-- tabs:start -->
54
58
55
59
### ** Python3**
You can’t perform that action at this time.
0 commit comments