http://leetcode.com/onlinejudge#question_4
Solution: a little ugly.
public class Solution {
public double findMedianSortedArrays(int A[], int B[]) {
// Start typing your Java solution below
// DO NOT write main() function
int len1 = A.length;
int len2 = B.length;
int i = 0;
int j = 0;
int k = 0;
double v = 0;
double pre = 0;
boolean odd = (len1 + len2) % 2 == 1;
int mid = (len1 + len2) / 2 + 1;
while(i<len1 && j<len2 && k < mid){
if(A[i] < B[j]) {
v = A[i];
if(k == mid-2){
pre = A[i];
}
i++;
}
else {
v = B[j];
if(k == mid-2){
pre = B[j];
}
j++;
}
k++;
}
if(k==mid){
if(odd){
return v;
}else {
return (double)(v + pre)/2;
}
}
while(i<len1 && k < mid){
v = A[i];
if(k==mid-2) pre = A[i];
i++;
k++;
}
if(k==mid){
if(odd){
return v;
}else {
return (double)(v + pre)/2;
}
}
while(j<len2 && k<mid){
v = B[j];
if(k==mid-2) pre = B[j];
j++;
k++;
}
if(k==mid){
if(odd){
return v;
}else {
return (double)(v + pre)/2;
}
}
return v;
}
}
分享到:
相关推荐
There are two sorted arrays nums1 and nums2 of size m and n respectively. ...Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Java AC版本
java java_leetcode题解之Median of Two Sorted Arrays.java
leetcode 无法登录两个有序数组的中位数 问题 有两个大小分别为 m 和 n 的排序数组 nums1 和 nums2。求两个排序数组的中位数。 整体运行时间复杂度应该是 O(log (m+n))。 您可以假设 nums1 和 nums2 不能都为空。 ...
c语言入门 c语言_leetcode题解04-median-of-two-sorted-arrays.c
4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 13. Roman to Integer 15. 3Sum 16. 3Sum Closest 17. Letter Combinations of a Phone Number 18. 4Sum ...
java入门 java_leetcode题解之004_Median_of_Two_Sorted_Arrays
js js_leetcode题解之4-median-of-two-sorted-arrays.js
leetcode Python 001 leetcode的算法问题 这是我的解决方案,用 cpp 、 java 和 python 编写 #LeetCode 解决的问题: 001. Two Sum 002. Add Two Numbers 003. Longest Substring Without Repeating Characters4. ...
leetcode分类LeetCodeSourceCodes###LeetCode1TowSum如果数组是有序的,O(n)的时间复杂度就可以解决了,现在是无序的,一开始要排一下序###LeetCode2MedianofTwoSortedArrays让用logn的时间复杂度,用了两个二分,...
lru缓存leetcode leetcode 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 5. Longest Palindromic Substring 7. Reverse Integer 9. ...
leetcode Python 001 力码 ├── Algorithms │ ├── cpp │ │ ├── 001 - Two Sum.cpp │ │ ├── 002 - Add Two Numbers.cpp │ │ ├── 003 - Longest Substring Without Repeating ...
7 Median of Two Sorted Arrays 33 8 Kth Largest Element in an Array 35 9 Wildcard Matching 37 10 Regular Expression Matching in Java 39 11 Merge Intervals 43 12 Insert Interval 45 13 Two Sum 47 14 Two ...
在LeetCode上,题目4(Median of Two Sorted Arrays)是一个经典的算法问题,旨在寻找两个已排序数组的中位数。中位数是将一组数值从小到大排列后处于中间位置的数,在奇数个数的情况下是中间的那个数,而在偶数个数...
leetcode 分类leetcode 问题分类 leetcode代码仓库,我的解题思路写在我的博客里: leetcode 代码库,我博客上的解题思路: mkdir 列表: 大批 #1:Two Sum #4:Median of Two Sorted Arrays 地图 #1:Two Sum #3:...
leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 重要 Longest Substring Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 ...
leetcode 2 Leetcode答案集 关于项目: 本项目包含本人LeetCode解题的答案,全部将由JavaScript语言进行解答。并会在每个题目的文件夹中添加相关的思路解析。 详情 # Title Solution Time Space Difficulty 1 Two ...
Median of Two Sorted Arrays 5 Longest Palindromic Substring 8 String to Integer 11 Container with Most Water 14 Longest Common Prefix 15 Three Sum 16 Three Sum Closest 20 Valid Parentheses 26 Remove ...
LeetCode刷题总结 1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7....
leetcode题库 LeetCode-Go 理论基础 见Note 脑图 TODO 待填充 算法题 面试高频出现,以及一些非常经典重要的算法题优先 题目列表 No Title Link Solution Acceptance Difficulty Topics Solution 0001 Two Sum 46.1%...
* 两个排序数组的中位数(Median of Two Sorted Arrays):计算两个排序数组的中位数。 * 整数到罗马(Integer to Roman):将整数转换为罗马数字。 * 罗马到整数(Roman to Integer):将罗马数字转换为整数。 4...