给定一个仅包含0和1 、大小为rows x cols的二维二进制矩阵,找出只包含1的最大矩形,并返回其面积。给定一个链表和一个特定值x,对链表进行分隔,使得所有小于x的节点都在大
于或等于x的节点之前。你应当保留两个分区中每个节点的初始相对位置。
- 示例:
输入: head = 1->4->3->2->5->2, x = 3
输出: 1->2->2->4->3->5解法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34object PartitionLinkedList {
def main(args: Array[String]): Unit = {
println(partition(ListNode(1, ListNode(4, ListNode(3, ListNode(2, ListNode(5, ListNode(2, null)))))), 3))
}
def partition(head: ListNode, x: Int): ListNode = {
if (head == null || head.next == null) return head
var first: ListNode = ListNode(-1, null)
val firstHead = first
var second: ListNode = ListNode(-1, null)
val secondHead: ListNode = second
var cur = head
while (cur != null) {
if (cur.x < x) {
first.next = cur
cur = cur.next
first = first.next
first.next = null
} else if (cur.x >= x) {
second.next = cur
cur = cur.next
second = second.next
second.next = null
}
}
first.next = secondHead.next
firstHead.next
}
case class ListNode(x: Int, var next: ListNode)
}