GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/java/004_Median_of_Two_Sorted_Arrays.java at master · yishangupenn/leetcode

Expand file tree

/

004_Median_of_Two_Sorted_Arrays.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
24 lines (24 loc) · 751 Bytes
/

004_Median_of_Two_Sorted_Arrays.java

Copy path

File metadata and controls

24 lines (24 loc) · 751 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Solution {
// example in leetcode book
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int p1 = 0, p2 = 0, pos = 0;
int ls1 = nums1.length, ls2 = nums2.length;
int[] all_nums = new int[ls1+ls2];
double median = 0.0;
while (p1 < ls1 && p2 < ls2){
if (nums1[p1] <= nums2[p2])
all_nums[pos++] = nums1[p1++];
else
all_nums[pos++] = nums2[p2++];
}
while (p1 < ls1)
all_nums[pos++] = nums1[p1++];
while (p2 < ls2)
all_nums[pos++] = nums2[p2++];
if ((ls1 + ls2) % 2 == 1)
median = all_nums[(ls1 + ls2) / 2];
else
median = (all_nums[(ls1 + ls2) / 2] + all_nums[(ls1 + ls2) / 2 - 1]) / 2.0;
return median;
}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.