Kth smallest element (in a sorted matrix ; In a BST tree ; In two sorted array)
来源:互联网 发布:webpackdevserver端口 编辑:程序博客网 时间:2024/06/06 01:42
包含了三个问题
1.Kth smallest element in a sorted matrix2.Kth smallest element in a BST(Binary Search Tree)
3.Kth smallest element in two sorted array
/** * 包含了三个问题 * 1.Kth smallest element in a sorted matrix * 2.Kth smallest element in a BST(Binary Search Tree) * 3.Kth smallest element in two sorted array * @author 健身小码哥 * */public class KthSmallestElement {/** * Kth smallest element in a sorted matrix * 思路一:二分查找 * @param matrix * @param k * @return */public int findKthSmallestElementInSortedMatrix(int matrix[][] , int k){int n = matrix.length; int startValue = matrix[0][0]; int endValue = matrix[n-1][n-1]; while (startValue < endValue) { int midValue = (startValue + endValue) / 2; int smallerCount = 0; for (int i = 0; i < n; i++) { for(int j = 0 ; j < n ; j++) { if(matrix[i][j] <= midValue) { smallerCount++; } }}if (smallerCount < k) {startValue = midValue + 1;} else {endValue = midValue; }}return startValue;}/** * Kth smallest element in a BST(Binary Search Tree) * @param root * @param k * @return */public int findKthSmallestElementInBST(TreeNode root , int k){if (root == null){return -1; }int leftSize = getTreeSize(root.left); if (k == leftSize+1){ return root.value; }else if (k <= leftSize){ return findKthElementInBST(root.left,k); }else{ return findKthElementInBST(root.right, k-leftSize-1); } }/** * 查找两个有序数组中第k小的数 * @param nums1有序数组1 * @param startIndex1数组1开始位置 * @param nums2有序数组2 * @param startIndex2数组2开始位置 * @param k * @return */public int findKthSmallestElementInTwoSortArray(int nums1[] ,int startIndex1, int nums2[] , int startIndex2 , int k){int m = nums1.length - startIndex1;int n = nums2.length - startIndex2;//确保数组2的长度较大,方便之后的操作if(m > n){return findKthSmallestElementInTwoSortArray(nums2, startIndex2, nums1, startIndex1, k);}if(m == 0){return nums2[k-1];}if(k <= 1){return Math.min(nums1[startIndex1], nums2[startIndex2]);}int minMid = Math.min(k/2,m);int maxMid = k - minMid;if(nums1[minMid + startIndex1 - 1] < nums2[maxMid +startIndex2 -1]){return findKthSmallestElementInTwoSortArray(nums1, startIndex1+minMid, nums2, startIndex2, k - minMid);}else if(nums1[minMid + startIndex1 - 1] > nums2[maxMid+startIndex2 -1]){return findKthSmallestElementInTwoSortArray(nums1, startIndex1, nums2, startIndex2 + maxMid, k - maxMid);}else{return nums1[minMid+startIndex1 -1];}}/** * * 获取树的大小(节点个数) * @param root根节点 * @return */public int getTreeSize(TreeNode root){ if (root == null){ return 0;}return 1+getTreeSize(root.left) + getTreeSize(root.right); } public static void main(String[] args) {int nums1[] = {1,3,5,7,9};int nums2[] = {2,4,6,8,10};int s = new KthSmallestElement().findKthSmallestElementInTwoSortArray(nums1, 0, nums2, 0, 6);System.out.println(s);}}/** * 树节点类 * @author 健身小码哥 * */class TreeNode{TreeNode left;TreeNode right;int value;}
阅读全文
0 0
- Kth smallest element (in a sorted matrix ; In a BST tree ; In two sorted array)
- Kth Smallest Element in a Sorted Matrix
- Kth Smallest Element in a Sorted Matrix
- Kth Smallest Element in a Sorted Matrix
- Kth Smallest Element in a Sorted Matrix
- Kth Smallest Element in a Sorted Matrix
- Kth Smallest Element in a Sorted Matrix
- 378. Kth Smallest Element in a Sorted Matrix
- 【Leetcode】Kth Smallest Element in a Sorted Matrix
- 378. Kth Smallest Element in a Sorted Matrix 解题报告
- LeetCode—378. Kth Smallest Element in a Sorted Matrix
- Leetcode 378. Kth Smallest Element in a Sorted Matrix
- [leetcode] 378. Kth Smallest Element in a Sorted Matrix
- LeetCode 378. Kth Smallest Element in a Sorted Matrix
- 【leetcode】378. Kth Smallest Element in a Sorted Matrix
- LeetCode[378] Kth Smallest Element in a Sorted Matrix
- LeetCode:378 Kth Smallest Element in a Sorted Matrix
- leetcode之 Kth Smallest Element in a Sorted Matrix
- produces在@requestMapping中的使用方式和作用
- 如何让ArrayList<Integer>添加一个String类型的元素呢
- c++ 90,91,92
- UE4添加枪口火焰效果
- springmvc学习-框架原理图
- Kth smallest element (in a sorted matrix ; In a BST tree ; In two sorted array)
- Python文件基本操作
- SPA
- hdoj1074 Doing Homework(好题呀,状态压缩+DP)
- 优先队列 CodeForces
- 软件设计之面向过程
- 一些常见的Oracle时间、日期的用法
- Strtus2与Spring-MVC的区别?
- X86 BSWAP指令详解