Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. class Solution {
  2. public double findMedianSortedArrays(int[] nums1, int[] nums2) {
  3. int arr[] = new int[nums1.length + nums2.length];
  4.  
  5.  
  6. int a = 0;
  7. int b = 0;
  8. int i = 0;
  9. while( a < nums1.length && b < nums2.length){
  10. if(nums1[a] <= nums2[b]){
  11. arr[i] = nums1[a];
  12. a++;
  13. }
  14. else{
  15. arr[i] = nums2[b];
  16. b++;
  17. }
  18. i++;
  19. }
  20.  
  21. while(a < nums1.length){
  22. arr[i] = nums1[a];
  23. i++;
  24. a++;
  25. }
  26.  
  27. while(b < nums2.length){
  28. arr[i] = nums2[b];
  29. i++;
  30. b++;
  31. }
  32.  
  33. if(arr.length % 2 == 1){
  34. return (double) arr[arr.length / 2];
  35. }
  36. else{
  37. int m1 = arr[(arr.length / 2) - 1];
  38. int m2 = arr[(arr.length / 2)];
  39. return (double) (m1 + m2) / 2;
  40. }
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement