`

<找工作 六>leetcode Median of Two Sorted Arrays

 
阅读更多

http://www.leetcode.com/onlinejudge

Median of Two Sorted Arrays

代码风格很乱,将就看吧,

写完才发现,写的有问题,没必要对end做操作,只操作start就可以了,类似归并

 

public class testMedianofTwoSortedArrays {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int A[]={};
		int B[]={2,3};
		double c=new testMedianofTwoSortedArrays().findMedianSortedArrays(A, B);
		System.out.println(c);
	}
	
	    public double findMedianSortedArrays(int A[], int B[]) {
	        // Start typing your Java solution below
	        // DO NOT write main() function        
	        int startA=0;
	        int startB=0;
	        int tempS=0,tempE=0;
	        int endA=A.length-1;
	        int endB=B.length-1;
	        double result=0;
	        while(true){
	            
	            if(startA>=A.length){
	                tempS=B[startB];
	                startB++;
	            
	            }else{
	            if(startB>=B.length){
	                tempS=A[startA];
	                startA++;
	            }
	            }
	            if(startA<A.length&&startB<B.length){
	            if(A[startA]>B[startB]){
	                tempS=B[startB];
	                startB++;
	            }else{                
	                tempS=A[startA];
	                startA++;
	            }
	            }
	            if(endA<0){
	                tempE=B[endB];
	                endB--;
	            }else{
	            if(endB<0){
	                tempE=A[endA];
	                endA--;
	            }
	            
	            }
	            if(endA>=0&&endB>=0){
	            if(A[endA]<B[endB]){
	                tempE=B[endB];
	                endB--;
	            }else{                
	                tempE=A[endA];
	                endA--;
	            }
	            
	            }
	            if (endA<startA&&endB<startB){
	                break;
	            }
	            
	            
	            
	        }
	        return ((double)tempS+tempE)/2;
	    
	    }
	}

 

改进后的,public class testMedianofTwoSortedArrays {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int A[] = {};
		int B[] = { 1, 2, 3, 4, 5 };
		double c = new testMedianofTwoSortedArrays().findMedianSortedArrays(A,
				B);
		System.out.println(c);
	}

	public double findMedianSortedArrays(int A[], int B[]) {
		// Start typing your Java solution below
		// DO NOT write main() function
		int startA = 0;
		int startB = 0;
		int tempS = 0, tempE = 0;
		double end = ((double) A.length + B.length) / 2;
		double result = 0;
		while (startA + startB < end) {
			if (startA >= A.length) {
				tempS = B[startB];
				startB++;
			} else if (startB >= B.length) {
				tempS = A[startA];
				startA++;
			} else if (A[startA] > B[startB]) {
				tempS = B[startB];
				startB++;
			} else {
				tempS = A[startA];
				startA++;
			}

		}
		if ((A.length + B.length) % 2 == 1) {
			tempE = tempS;

		} else if (startA >= A.length) {
			tempE = B[startB];

		} else if (startB >= B.length) {
			tempE = A[startA];

		} else if (A[startA] > B[startB]) {
			tempE = B[startB];

		} else {
			tempE = A[startA];

		}
		return ((double) tempS + tempE) / 2;

	}
}
 
分享到:
评论

相关推荐

    LeetCode4 Median of Two Sorted Arrays

    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-leetcode题解之Median of Two Sorted Arrays.java

    java java_leetcode题解之Median of Two Sorted Arrays.java

    java-leetcode题解之004-Median-of-Two-Sorted-Arrays

    java入门 java_leetcode题解之004_Median_of_Two_Sorted_Arrays

    c语言-leetcode题解04-median-of-two-sorted-arrays.c

    c语言入门 c语言_leetcode题解04-median-of-two-sorted-arrays.c

    js-leetcode题解之4-median-of-two-sorted-arrays.js

    js js_leetcode题解之4-median-of-two-sorted-arrays.js

    程序员面试宝典LeetCode刷题手册

    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 ...

    leetcode无法登录-MedianOfTwoSortedArrays:双排序数组的中位数

    leetcode 无法登录两个有序数组的中位数 问题 有两个大小分别为 m 和 n 的排序数组 nums1 和 nums2。求两个排序数组的中位数。 整体运行时间复杂度应该是 O(log (m+n))。 您可以假设 nums1 和 nums2 不能都为空。 ...

    leetcodepython001-algorithm:leetcode问题(cpp、java、python),书籍破解_the_coding

    leetcode Python 001 leetcode的算法问题 这是我的解决方案,用 cpp 、 java 和 python 编写 #LeetCode 解决的问题: 001. Two Sum 002. Add Two Numbers 003. Longest Substring Without Repeating Characters4. ...

    lrucacheleetcode-leetcode:leetcode

    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. ...

    leetcodepython001-leetcode:https://leetcode.com/的解决方案

    leetcode Python 001 力码 ├── Algorithms │  ├── cpp │  │  ├── 001 - Two Sum.cpp │  │  ├── 002 - Add Two Numbers.cpp │  │  ├── 003 - Longest Substring Without Repeating ...

    Coding Interview In Java

    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分类-leetcode:leetcode问题的代码

    leetcode 分类leetcode 问题分类 leetcode代码仓库,我的解题思路写在我的博客里: leetcode 代码库,我博客上的解题思路: mkdir 列表: 大批 #1:Two Sum #4:Median of Two Sorted Arrays 地图 #1:Two Sum #3:...

    LeetCode 刷题汇总1

    * 两个排序数组的中位数(Median of Two Sorted Arrays):计算两个排序数组的中位数。 * 整数到罗马(Integer to Roman):将整数转换为罗马数字。 * 罗马到整数(Roman to Integer):将罗马数字转换为整数。 4...

    leetcode2-Leetcode:Leetcode_answer

    leetcode 2 Leetcode答案集 关于项目: 本项目包含本人LeetCode解题的答案,全部将由JavaScript语言进行解答。并会在每个题目的文件夹中添加相关的思路解析。 详情 # Title Solution Time Space Difficulty 1 Two ...

    leetcode双人赛-LeetCode:力扣笔记

    leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 重要 Longest Substring Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 ...

    javalruleetcode-LeetCode::lollipop:个人LeetCode习题解答仓库-多语言

    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

    4. **Median of Two Sorted Arrays** (Hard) 这是一道查找两个已排序数组中中位数的题目。要求整体时间复杂度为O(log (m+n))。可以采用二分查找的方法,不断缩小查找范围,找到两个数组合并后中间位置的元素,或者...

    leetcode分类-LeetCodeSrc:力扣

    leetcode分类LeetCodeSourceCodes###LeetCode1TowSum如果数组是有序的,O(n)的时间复杂度就可以解决了,现在是无序的,一开始要排一下序###LeetCode2MedianofTwoSortedArrays让用logn的时间复杂度,用了两个二分,...

    leetcode338-LeetCode: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 (Manacher算法待完成) 6.ZigZag Conversion 7....

    leetcode题库-LeetCode-Go:用GO回答LeetCode

    leetcode题库 LeetCode-Go 理论基础 见Note 脑图 TODO 待填充 算法题 面试高频出现,以及一些非常经典重要的算法题优先 题目列表 No Title Link Solution Acceptance Difficulty Topics Solution 0001 Two Sum 46.1%...

Global site tag (gtag.js) - Google Analytics