Median of Two Sorted Arrays
来源:互联网 发布:求顶尖数据恢复注册码 编辑:程序博客网 时间:2024/06/11 05:17
public class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
boolean flag=true;
int a=nums1.length/2;
int b=nums2.length/2;
int c=Math.abs(a-b);
int d=0;
if(nums1.length==0){
d=nums2[b];
}else if(nums2.length==0){
d=nums1[a];
}else{
while (flag) {
if (c<=1) {
flag=false;
if ((nums1[a]-nums2[b])*(Math.abs(nums1.length+nums2.length-2*a-2*b-3)-1)>0) {
d=nums2[b];
}else {
d=nums1[a];
}
}else if ((nums1[a]-nums2[b])>0&&((a-Math.max(0, a-c))>0||(nums1.length+nums2.length-2*a-2*b-2)>0)) {
c=((a-Math.max(0, a-c))+(Math.min(nums2.length-1,b+c)-b))/2;
a=Math.max(0, a-Math.min(1, c));
b=Math.min(nums2.length-1,b+Math.min(1, c));
}else {
c=((Math.min(nums1.length-1,a+c)-a)+(b-Math.max(0, b-c)))/2;
a=Math.min(nums1.length-1,a+Math.min(1, c));
b=Math.max(0, b-Math.min(1, c));
}
}
}
return (double)d;
}
}
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
boolean flag=true;
int a=nums1.length/2;
int b=nums2.length/2;
int c=Math.abs(a-b);
int d=0;
if(nums1.length==0){
d=nums2[b];
}else if(nums2.length==0){
d=nums1[a];
}else{
while (flag) {
if (c<=1) {
flag=false;
if ((nums1[a]-nums2[b])*(Math.abs(nums1.length+nums2.length-2*a-2*b-3)-1)>0) {
d=nums2[b];
}else {
d=nums1[a];
}
}else if ((nums1[a]-nums2[b])>0&&((a-Math.max(0, a-c))>0||(nums1.length+nums2.length-2*a-2*b-2)>0)) {
c=((a-Math.max(0, a-c))+(Math.min(nums2.length-1,b+c)-b))/2;
a=Math.max(0, a-Math.min(1, c));
b=Math.min(nums2.length-1,b+Math.min(1, c));
}else {
c=((Math.min(nums1.length-1,a+c)-a)+(b-Math.max(0, b-c)))/2;
a=Math.min(nums1.length-1,a+Math.min(1, c));
b=Math.max(0, b-Math.min(1, c));
}
}
}
return (double)d;
}
}
0 0
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of two sorted arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- median-of-two-sorted-arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- Median of Two Sorted Arrays
- MySQL分页查询
- Android中px、dp、dip、sp详解
- 避免TableViewCell重叠的问题
- 微软100题(53)字符串的排列
- MySQL之——新安装的MySQL必须调整的10项配置(插曲)
- Median of Two Sorted Arrays
- 回收站功能在 Linux 中的实现
- 工厂方法--创建型模式(Factory method)
- <body style="overflow-y:hidden;">
- 城市定位,利用百度地图API定位到你当前的位置
- mysql一: 索引优化
- Oracle中merge into的使用
- BZOJ3884 上帝与集合的正确用法(欧拉函数)
- python2.7.8在windows下安装numpy,scipy,matplotlib