赞
踩
题目链接: https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.
Note that it is the kth smallest element in the sorted order, not the kth distinct element.
Example:
matrix = [ [ 1, 5, 9], [10, 11, 13], [12, 13, 15] ], k = 8,
return 13.
Note:
You may assume k is always valid, 1 ≤ k ≤ n2.
自定义有序链表,从小大排列
实现对应的删除和插入操作
为什么第2.1步骤是第i小元素下方的元素
假设第i小元素为Axy,则加入链表中的元素为Ax+1y
Ax-1y | Ax-1y-1 |
---|---|
Axy | Axy+1 |
Ax+1y | Ax+1y+1 |
直接用链表实现的,插入的时候从头开始遍历链表
其实可以用二分查找进行优化插入位置
class ListNode { int val; int x; int y; ListNode next; ListNode(int val, int x, int y) { this.val = val; this.x = x; this.y = y; } } public ListNode rmTop(ListNode root) { return root.next; } public void insetList(ListNode root, ListNode node) { ListNode p = root, q = root.next; while (q != null && q.val <= node.val) { p = q; q = q.next; } p.next = node; if (q != null) { node.next = q; } } public int kthSmallest(int[][] matrix, int k) { if (matrix == null || matrix.length <= 0 || matrix[0].length <= 0) return -1; // 第一行放入有序列表中 ListNode root = new ListNode(matrix[0][0], 0, 0); ListNode tmp = root; for (int i = 1; i < matrix[0].length; i++) { ListNode node = new ListNode(matrix[0][i], 0, i); tmp.next = node; tmp = node; } for (int i = 1; i < k; i++) { if (root.x + 1 >= matrix[0].length) { root = rmTop(root); continue; } ListNode node = new ListNode(matrix[root.x + 1][root.y], root.x + 1, root.y); // 将node插入有序列表 insetList(root, node); // 删除top i节点 root = rmTop(root); } return root.val; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。