leetcode Median of Two Sorted Arrays

这篇具有很好参考价值的文章主要介绍了leetcode Median of Two Sorted Arrays。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.

The overall run time complexity should be O(log (m+n)).

Example 1:

Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
Explanation: merged array = [1,2,3] and median is 2.

Example 2:

Input: nums1 = [1,2], nums2 = [3,4]
Output: 2.50000
Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.

Constraints:文章来源地址https://www.toymoban.com/news/detail-400785.html

  • nums1.length == m
  • nums2.length == n
  • 0 <= m <= 1000
  • 0 <= n <= 1000
  • 1 <= m + n <= 2000
  • -106 <= nums1[i], nums2[i] <= 106
import java.util.*;

class Solution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        //int数组转为Integer类型数组
        //将int数组转换为数值流-》流中的元素全部装箱,转换为Integer流-》将流转换为数组
        Integer newNums1[] = Arrays.stream(nums1).boxed().toArray(Integer[]::new);
        Integer newNums2[] = Arrays.stream(nums2).boxed().toArray(Integer[]::new);
       ArrayList<Integer> integer = new ArrayList(Arrays.asList(newNums1));
       integer.addAll(Arrays.asList(newNums2));
       Collections.sort(integer);
        DecimalFormat df=new DecimalFormat("0.0");//设置保留位数
       if(integer.size()%2!=0){
          return  integer.get(integer.size()/2);
       }else {
          String s = df.format((integer.get(integer.size()/2) + integer.get(integer.size()/2 -1))/2.0);
         double out =  Double.parseDouble(s);
         return out;
       }
    }
}

到了这里,关于leetcode Median of Two Sorted Arrays的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode //88. Merge Sorted Array

    You are given two integer arrays nums1 and nums2 , sorted in non-decreasing order, and two integers m and n , representing the number of elements in nums1 and nums2 respectively. Merge nums1 and nums2 into a single array sorted in non-decreasing order. The final sorted array should not be returned by the function, but instead be stored inside the array nums1

    2024年02月12日
    浏览(45)
  • LeetCode 1385. Find the Distance Value Between Two Arrays

    Given two integer arrays  arr1  and  arr2 , and the integer  d ,  return the distance value between the two arrays . The distance value is defined as the number of elements  arr1[i]  such that there is not any element  arr2[j]  where  |arr1[i]-arr2[j]| = d . Example 1: Example 2: Example 3: Constraints: 1 = arr1.length, arr2.length = 500 -1000 = ar

    2024年02月10日
    浏览(43)
  • LeetCode83. Remove Duplicates from Sorted List

    Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. Example 1: Input: head = [1,1,2] Output: [1,2] Example 2: Input: head = [1,1,2,3,3] Output: [1,2,3] Constraints: The number of nodes in the list is in the range [0, 300]. -100 = Node.val = 100 The list is guaranteed t

    2024年01月18日
    浏览(48)
  • LeetCode //80. Remove Duplicates from Sorted Array II

    Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same. Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums

    2024年02月13日
    浏览(49)
  • Leetcode 82. Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List II Medium Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well. Example 1: Input: head = [1,2,3,3,4,4,5] Output: [1,2,5] Example 2: Input: head = [1,1,1,2,3] Output: [2,3] Constraints: The number of n

    2024年02月10日
    浏览(45)
  • LeetCode //C - 82. Remove Duplicates from Sorted List II

    Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list . Return the linked list sorted as well.   Example 1: Input: head = [1,2,3,3,4,4,5] Output: [1,2,5] Example 2: Input: head = [1,1,1,2,3] Output: [2,3] Constraints: The number of nodes in the list is in the range [0, 300]

    2024年02月10日
    浏览(45)
  • LeetCode //C - 153. Find Minimum in Rotated Sorted Array

    Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: [4,5,6,7,0,1,2] if it was rotated 4 times. [0,1,2,4,5,6,7] if it was rotated 7 times. Notice that rotating an array [a[0], a[1], a[2], …, a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], …, a[n

    2024年02月06日
    浏览(35)
  • Algorithms practice:leetcode 33. Search in Rotated Sorted Array

    Algorithms practice:leetcode33 Search in Rotated Sorted Array There is an integer array ,nums , sorted in ascending order (with distinct values). Prior to being passed to your function, nums is possibly rotated,at an unknown pivot index k (1 = k nums.length) such that the resulting array is [nums[k], nums[k+1], …, nums[n-1], nums[0], nums[1], …, nums

    2024年01月21日
    浏览(49)
  • 【LeetCode 算法】Merge k Sorted Lists 合并 K 个升序链表

    给你一个链表数组,每个链表都已经按升序排列。 请你将所有链表合并到一个升序链表中,返回合并后的链表。 k = = l i s t s . l e n g t h 0 = k = 1 0 4 0 = l i s t s [ i ] . l e n g t h = 500 − 1 0 4 = l i s t s [ i ] [ j ] = 1 0 4 l i s t s [ i ] 按升序排列 l i s t s [ i ] . l e n g t h 的总和不超过 1 0

    2024年02月13日
    浏览(34)
  • LeetCode //2723. Add Two Promises (Day 30 of LC JavaScript Challenage)

    Given two promises promise1 and promise2 , return a new promise. promise1 and promise2 will both resolve with a number. The returned promise should resolve with the sum of the two numbers.   Example 1: Input: promise1 = new Promise(resolve = setTimeout(() = resolve(2), 20)), promise2 = new Promise(resolve = setTimeout(() = resolve(5), 60)) Output: 7 Explana

    2024年02月11日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包