现有两个从小到大有序非空数组A,B,你需要输出数组中,A数组长度为n,B数组长度为m 你需要找出这两个数组中第K大数的数值-----ans,即将A,B归并成一个长度为(n+m)的从小到大排序的数组C,C[n+m-K]即是答案
示例1
输入
2,[1,2],3,[3,4,5],5
输出
1
说明
A,B合并后成为新数组C,C数组是[1,2,3,4,5],C[0]为1,所以答案是1
备注:
请用时间O(log(K)),空间O(1)算法实现
加载中...
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 找K大函数 * @param n int整型 A数组长度 * @param A int整型一维数组 A数组 * @param m int整型 B数组长度 * @param B int整型一维数组 B数组 * @param K int整型 第几大 * @return int整型 */ public int findKthInTwoSortedArray (int n, int[] A, int m, int[] B, int K) { // write code here } }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 找K大函数 * @param n int整型 A数组长度 * @param A int整型vector A数组 * @param m int整型 B数组长度 * @param B int整型vector B数组 * @param K int整型 第几大 * @return int整型 */ int findKthInTwoSortedArray(int n, vector
& A, int m, vector
& B, int K) { // write code here } };
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # 找K大函数 # @param n int整型 A数组长度 # @param A int整型一维数组 A数组 # @param m int整型 B数组长度 # @param B int整型一维数组 B数组 # @param K int整型 第几大 # @return int整型 # class Solution: def findKthInTwoSortedArray(self , n , A , m , B , K ): # write code here
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 找K大函数 * @param n int整型 A数组长度 * @param A int整型一维数组 A数组 * @param m int整型 B数组长度 * @param B int整型一维数组 B数组 * @param K int整型 第几大 * @return int整型 */ function findKthInTwoSortedArray( n , A , m , B , K ) { // write code here } module.exports = { findKthInTwoSortedArray : findKthInTwoSortedArray };
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # 找K大函数 # @param n int整型 A数组长度 # @param A int整型一维数组 A数组 # @param m int整型 B数组长度 # @param B int整型一维数组 B数组 # @param K int整型 第几大 # @return int整型 # class Solution: def findKthInTwoSortedArray(self , n , A , m , B , K ): # write code here
package main /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 找K大函数 * @param n int整型 A数组长度 * @param A int整型一维数组 A数组 * @param m int整型 B数组长度 * @param B int整型一维数组 B数组 * @param K int整型 第几大 * @return int整型 */ func findKthInTwoSortedArray( n int , A []int , m int , B []int , K int ) int { // write code here }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 找K大函数 * @param n int整型 A数组长度 * @param A int整型一维数组 A数组 * @param ALen int A数组长度 * @param m int整型 B数组长度 * @param B int整型一维数组 B数组 * @param BLen int B数组长度 * @param K int整型 第几大 * @return int整型 */ int findKthInTwoSortedArray(int n, int* A, int ALen, int m, int* B, int BLen, int K ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # 找K大函数 # @param n int整型 A数组长度 # @param A int整型一维数组 A数组 # @param m int整型 B数组长度 # @param B int整型一维数组 B数组 # @param K int整型 第几大 # @return int整型 # class Solution def findKthInTwoSortedArray(n, A, m, B, K) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 找K大函数 * @param n int整型 A数组长度 * @param A int整型一维数组 A数组 * @param m int整型 B数组长度 * @param B int整型一维数组 B数组 * @param K int整型 第几大 * @return int整型 */ def findKthInTwoSortedArray(n: Int,A: Array[Int],m: Int,B: Array[Int],K: Int): Int = { // write code here } }
2,[1,2],3,[3,4,5],5
1