leetcode4题 题解 翻译 C语言版 Python版

Allegro PCB设计小技巧:如何用Shift键快速平移复制走线和过孔? 本文深入解析了Allegro PCB设计中利用Shift键进行平移复制的核心技巧。通过对比传统复制粘贴流程,详细演示了如何高效、精准地批量复制走线和过孔,尤其适用于创建等间距电源总线、过孔阵列等场景,能显著提升PCB设计效率与准确性。 阅读详情

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

Example 1:

nums1 = [1, 3]
nums2 = [2]

The median is 2.0

Example 2:

nums1 = [1, 2]
nums2 = [3, 4]

The median is (2 + 3)/2 = 2.5
4. 两有序数组的中位数

有两个有序数组nums1和nums2,各自长度为m和n。

找到两个数组的中位数,整体的运行时间复杂度应该是O(log (m+n))。

例1:

nums1 = [1, 3]
nums2 = [2]

两数组的中位数是 2.0
例2:

nums1 = [1, 2]
nums2 = [3, 4]

两数组的中位数是 (2 + 3)/2 = 2.5

思路:

最简单的想法是合并两数组排序合取中位数,但是要排序的话肯定不满足题目要求的时间复杂度。那么可以借助两数组分别有序的特性,在合并过程中始终保持有序,也即是归并排序。合并完后找到中间两个数或中间一个数即可。

另一种思路是不合并数组,每次从两数组头部中选较小的去掉,从两数组尾部中选较大的去掉,直到最后就可以确定中位数了。

两种思路运行时间差不多,思路2不需要额外内存。


思路1C语言版:

double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
    int numsSize = nums1Size + nums2Size;
    int *nums = (int*)malloc(sizeof(int) * numsSize);
    int min, max;
    for (int i = 0, j = 0, k = 0; k < numsSize; ){
        if (i >= nums1Size){
            nums[k++] = nums2[j++];
        }
        else if (j >= nums2Size){
            nums[k++] = nums1[i++];
        }
        else {
            nums[k++] = nums1[i] < nums2[j] ? nums1[i++] : nums2[j++];
        }
    }
    if (numsSize % 2 == 0){
        min = nums[numsSize / 2 - 1];
        max = nums[numsSize / 2];
    }
    else {
        min = max = nums[numsSize / 2];
    }
    free(nums);
    return (double)(min + max) / 2;
}

思路2C语言版:

double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
    int min1 = 0, min2 = 0, max1 = nums1Size - 1, max2 = nums2Size - 1;
    int mid_min, mid_max;
    for(;max1 >= min1 || max2 >= min2;){    //至少有一条链还存在
        if (min1 > max1){   //如果链1不存在了
            mid_min = nums2[min2];  //取链2头尾
            mid_max = nums2[max2];
            min2++; //去掉链2头尾
            max2--;
        }
        else if (min2 > max2){  //如果链2不存在了
            mid_min = nums1[min1];  //取链1头尾
            mid_max = nums1[max1];
            min1++; //去年链1头尾
            max1--;
        }
        else{   //如果两链都存在
            if (nums1[min1] < nums2[min2]){ //如果链1头部最小
                mid_min = nums1[min1];  //取链1头部
                min1++; //去掉链1头部
            }
            else{   //如果链2头部最小
                mid_min = nums2[min2];  //取链2头部
                min2++; //去掉链2头部
            }
            //在去掉一个头部后可能出现某链为空的情况
            if (min1 > max1){   //如果链1变空
                mid_max = nums2[max2];  //取链2尾部
                max2--; //去掉链2尾部
            }
            else if (min2 > max2){  //如果链2变空
                mid_max = nums1[max1];  //取链1尾部
                max1--; //去掉链1尾部
            }
            //如果两链都不为空
            else if (nums1[max1] > nums2[max2]){ //如果链1尾部最大
                mid_max = nums1[max1];  //取链1尾部
                max1--; //去掉链1尾部
            }
            else{   //如果链2尾部最大
                mid_max = nums2[max2];  //取链2尾部
                max2--; //去掉链2尾部
            }
        }
    }
    return (double)(mid_min + mid_max) / 2;
}



思路1Python版:

class Solution(object):
    def findMedianSortedArrays(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: float
        """
        nums = [0] * (len(nums1) + len(nums2))  # 合并列表
        i, j, k = 0, 0, 0
        while k < len(nums):    # 循环合并
            if i >= len(nums1): # 如果只剩列表2
                nums[k] = nums2[j]  # 添加列表2的元素
                j += 1
            elif j >= len(nums2):   # 如果只剩列表1的元素
                nums[k] = nums1[i]  # 添加列表1的元素
                i += 1
            else:   # 如果两列表都存在
                if nums1[i] < nums2[j]: # 取两列表头部较小值
                    nums[k] = nums1[i]
                    i += 1
                else:
                    nums[k] = nums2[j]
                    j += 1 
            k += 1  # 移动合并后列表的游标
        mi, ma = 0, 0
        if len(nums) % 2 == 0:  # 如果合并后的列表偶数个元素
            mi = nums[len(nums) / 2 - 1]
            ma = nums[len(nums) / 2]
        else:   # 奇数个
            mi = ma = nums[len(nums) / 2]
        return (float)(mi + ma) / 2












Ut接口原理及 VoLTE 补充业务自管理信令流程(GBA 引导认证+业务访问认证+ VoLTE 补充业务自管理) 2. Ut 接口及 VoLTE 业务子管理概述 2.1 UT 接口 —— VoLTE 终端补充业务配置 2.2终端通过 Ut 接口完成 VoLTE 补充业务自管理举例 2.3什么是 GBA 和 GAA 架构 2.4为什么要引入 GBA 架构认证及 BSF 网元 2.5UE(终端) 接口相关网元及功能 2.6GBA 架构流程概述 3. Ut 接口业务自管理信令流程 3.1 补充业务自管理及本文主要参考以下规范 3.2 Ut 接口业务自管理信令流程:查询补充业务 3.2.1 UE获 阅读详情

相关推荐

Mac OS 安装 finalshell

Mac OS 安装 finalshell安装包

leetcode.4寻找两个有序数组的中位数(C语言

1、问描述 这道的边界判断比较的多,如果是边测试边界边编写判断语句的话,一点点的总能成功(我就是这样),如果要一口气分析的话属实有点麻烦。 下面是具体思路: 1、我们总能用一刀将两个数组各切成两半。 切完之后呢,在nums1中,切线左边所有元素肯定小于右边所有元素,即L_MAX1 <= R_MIN1 同样,在nums2中,L_MAX2 <= R_MIN2 。这个毋庸置疑的。 ...

weixin_38072112的博客 700

python实现在线翻译

主要介绍了python实现在线翻译,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

pythonleetcode 目(45)

两整数之和不使用运算符 + 和-,计算两整数a 、b之和。示例:若 a = 1 ,b = 2,返回 3。代码:class Solution: def getSum(self, a, b): """ :type a: int :type b: int :rtype: int """ # retur...

凌墨的博客 586

leetcode 4(python实现)

leetcode 4 目描述 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)). You may assu...

qq_43271202的博客 858

Python | Leetcode Python题解之第45跳跃游戏II

Python | Leetcode Python题解之第45跳跃游戏II

Mopes__的博客 641

LeetCode Python - 45.跳跃游戏②

n−2] 的每一个位置 i,对于每一个位置 i,我们可以通过 i+nums[i] 计算出当前位置能够到达的最远位置,我们用 mx 来记录这个最远位置,即 mx=max(mx,i+nums[i])。接下来,判断当前位置是否到达了上一次跳跃的边界,即 i=last,如果到达了,那么我们就需要进行一次跳跃,将 last 更新为 mx,并且将跳跃次数 ans 增加 1。我们可以用变量 mx 记录当前位置能够到达的最远位置,用变量 last 记录上一次跳跃到的位置,用变量 ans 记录跳跃的次数。

xuxu96 514

leetcode 题解 翻译 C语言 Python 合集 (不断更新)

leetcode100 题解 翻译 C语言版 Python leetcode104 题解 翻译 C语言版 Python leetcode171 题解 翻译 C语言版 Python leetcode226 题解 翻译 C语言版 Python leetcode237 题解 翻译 C语言版 Python leetcode242 题解 翻译 C语言版 Pyt

陈止风的博客 2342

leetcode2 题解 翻译 C语言版 Python

2. Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbe

陈止风的博客 3737

leetcode226 题解 翻译 C语言版 Python

226. Invert Binary Tree Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem was inspired by this

陈止风的博客 1333

leetcode101 题解 翻译 C语言版 Python

101. Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \

陈止风的博客 1913

Python世界:力扣题解875,珂珂爱吃香蕉,中等

最小速度能保证吃完,但耗时最大,最大速度能一定吃完,耗时最小。可初步判断为线上python2.x较老,整数相除模拟的是C实现,而线下python3.x较新,整数相除不尽结果是浮点。最小速度,若取数组中的最小值去吃,作为最慢速度吃,假如时间足够长,可能还不够慢。翻译下,需求是:对给定无序数组表示N堆香蕉,找到最小吃香蕉的速度k,且在h小时内吃完。最大速度,可取数组中的最大值,则数组的长度即为耗时,而已知条件数组的长度len<=h。但出现一个神奇的现象是,本地通过,但提交线上通不过,实在奇怪。

来知晓的博客 1407

leetcode112 题解 翻译 C语言版 Python

112. Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below bin

陈止风的博客 1120

leetcode292 题解 翻译 C语言版 Python

292. Nim Game You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the las

陈止风的博客 862

leetcode237 题解 翻译 C语言版 Python

237. Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and

陈止风的博客 1127

c语言错误237,leetcode237 题解 翻译 C语言版 Python

237. Delete Node in a Linked ListWrite a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is1 -> 2 -> 3 -> 4and y...

weixin_42518480的博客 250

LeetCode第四十五Python实现

title: LeetCode No.45 categories: OJ LeetCode tags: Programing LeetCode OJ LeetCode第四十五 自己的开源仓库:click here 目描述 给定一个非负整数数组,你最初位于数组的第一个位置。 数组中的每个元素代表你在该位置可以跳跃的最大长度。 你的目标是使用最少的跳跃次数到达数组的最后一个位置。 示例: 输入: [2,3,1,1,4] 输出: 2 解释: 跳到最后一个位置的最小跳跃数是 2。 从下标为 0 跳到.

StriveZs'Blog 344

LeetCode45. Jump Game II 解报告(Python

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录目描述目大意解方法贪心日期 目地址:https://leetcode.com/problems/reach-a-number/description/ 目描述 Given an array of non-negative integers, you are initial...

负雪明烛 4258

LeetCode集四(Python实现

LeetCode集四简单121. 买卖股票的最佳时机 简单 121. 买卖股票的最佳时机 目: 思路: 先将第一个值默认是最小的价钱,之后依次查找后面的价格,进行对比获取最小价格和最大收益。 解法: class Solution: def maxProfit(self, prices: List[int]) -> int: if len(prices) < 2: return 0 res = 0

bansme的博客 427

PythonCode】力扣Leetcode41~45Python

力扣40-45

weixin_43790276的博客 1309

力扣(leetcode)第4寻找两个正序数组的中位数Python

最后,我写了一篇MySQL教程,里面详细的介绍了MySQL的基本概念以及操作指令等内容,欢迎阅读!解释:合并数组 = [1,2,3,4] ,中位数 (2 + 3) / 2 = 2.5。输入:nums1 = [1,2], nums2 = [3,4]输入:nums1 = [1,3], nums2 = [2]解释:合并数组 = [1,2,3] ,中位数 2。请你找出并返回这两个正序数组的 中位数。的正序(从小到大)数组。算法的时间复杂度应该为。输出:2.00000。输出:2.50000。

qq_58737789的博客 1241
上一篇: leetcode3题 题解 翻译 C语言版 Python版
下一篇: CENTOS防火墙简单操作
陈止风
博客等级 码龄14年 86粉丝 96原创
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值