当前位置: 首页 > news >正文

东营 微信网站建设百度指数行业排行

东营 微信网站建设,百度指数行业排行,建行官方网站多少,义乌网站建设微信开发【LetMeFly】88.合并两个有序数组:O(m 1) O(1)的做法 力扣题目链接:https://leetcode.cn/problems/merge-sorted-array/ 给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2…

【LetMeFly】88.合并两个有序数组:O(m + 1) + O(1)的做法

力扣题目链接:https://leetcode.cn/problems/merge-sorted-array/

给你两个按 非递减顺序 排列的整数数组 nums1 nums2,另有两个整数 mn ,分别表示 nums1nums2 中的元素数目。

请你 合并 nums2 nums1 中,使合并后的数组同样按 非递减顺序 排列。

注意:最终,合并后数组不应由函数返回,而是存储在数组 nums1 中。为了应对这种情况,nums1 的初始长度为 m + n,其中前 m 个元素表示应合并的元素,后 n 个元素为 0 ,应忽略。nums2 的长度为 n

 

示例 1:

输入:nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
输出:[1,2,2,3,5,6]
解释:需要合并 [1,2,3] 和 [2,5,6] 。
合并结果是 [1,2,2,3,5,6] ,其中斜体加粗标注的为 nums1 中的元素。

示例 2:

输入:nums1 = [1], m = 1, nums2 = [], n = 0
输出:[1]
解释:需要合并 [1] 和 [] 。
合并结果是 [1] 。

示例 3:

输入:nums1 = [0], m = 0, nums2 = [1], n = 1
输出:[1]
解释:需要合并的数组是 [] 和 [1] 。
合并结果是 [1] 。
注意,因为 m = 0 ,所以 nums1 中没有元素。nums1 中仅存的 0 仅仅是为了确保合并结果可以顺利存放到 nums1 中。

 

提示:

  • nums1.length == m + n
  • nums2.length == n
  • 0 <= m, n <= 200
  • 1 <= m + n <= 200
  • -109 <= nums1[i], nums2[j] <= 109

 

进阶:你可以设计实现一个时间复杂度为 O(m + n) 的算法解决此问题吗?

方法一:三指针(双指针)

这道题不返回任何值,很显然,出题者想让你在nums1数组上原地修改。

怎么原地修改呢?nums1后面全是 0 0 0,而这些地方本来应该是“大数”,所以我们使用两个指针,从 n u m s 1 nums1 nums1 n u m s 2 nums2 nums2的大数区域往前指,每次将二者较大的那个放到nums1后面不就可以了吗。

      tail↓
1 3 0 0↑
2 6↑

3 < 6 3 < 6 3<6,所以将 6 6 6放到tail处,

    tail↓
1 3 0 6↑
2 -
↑

3 > 2 3 > 2 3>2,所以将 3 3 3放到tail处,

  tail↓
1 - 3 6
↑
2 -
↑

1 < 2 1 < 2 1<2,所以将 2 2 2放到tail处,

tail
↓
1 2 3 6
↑
- -

n u m s 2 nums2 nums2的指针指完了,任务完成,得到 [ 1 , 2 , 3 , 6 ] [1, 2, 3, 6] [1,2,3,6]

  • 时间复杂度 O ( m + n ) O(m + n) O(m+n)
  • 空间复杂度 O ( 1 ) O(1) O(1)

AC代码

C++

class Solution {
public:void merge(vector<int>& nums1, int l1, vector<int>& nums2, int l2) {int n = l1 + l2 - 1;l1--, l2--;while (l2 >= 0) {while (l1 >= 0 && nums1[l1] > nums2[l2]) {nums1[n--] = nums1[l1--];}nums1[n--] = nums2[l2--];}}
};

Python

from typing import Listclass Solution:def merge(self, nums1: List[int], l1: int, nums2: List[int], l2: int) -> None:"""Do not return anything, modify nums1 in-place instead."""l = l1 + l2 - 1l1, l2 = l1 - 1, l2 - 1while l2 >= 0:while l1 >= 0 and nums1[l1] > nums2[l2]:nums1[l] = nums1[l1]l, l1 = l - 1, l1 - 1nums1[l] = nums2[l2]l, l2 = l - 1, l2 - 1

同步发文于CSDN,原创不易,转载经作者同意后请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/132256535


文章转载自:
http://pinyin.fwrr.cn
http://pessary.fwrr.cn
http://bilk.fwrr.cn
http://eosinophil.fwrr.cn
http://shop.fwrr.cn
http://mythical.fwrr.cn
http://renewed.fwrr.cn
http://lunchhook.fwrr.cn
http://duckfooted.fwrr.cn
http://granivore.fwrr.cn
http://ethnolinguistics.fwrr.cn
http://mowing.fwrr.cn
http://sheepshead.fwrr.cn
http://archenemy.fwrr.cn
http://emptiness.fwrr.cn
http://klavern.fwrr.cn
http://gall.fwrr.cn
http://ochlocracy.fwrr.cn
http://chutnee.fwrr.cn
http://darkminded.fwrr.cn
http://hemocytoblastic.fwrr.cn
http://hemiplegy.fwrr.cn
http://synonym.fwrr.cn
http://agronomic.fwrr.cn
http://arrear.fwrr.cn
http://overswing.fwrr.cn
http://hamam.fwrr.cn
http://radioprotector.fwrr.cn
http://skidoo.fwrr.cn
http://crackback.fwrr.cn
http://inferiority.fwrr.cn
http://chaffer.fwrr.cn
http://whiskerage.fwrr.cn
http://dressing.fwrr.cn
http://preoccupant.fwrr.cn
http://pulmotor.fwrr.cn
http://syphilology.fwrr.cn
http://ninon.fwrr.cn
http://intonation.fwrr.cn
http://confide.fwrr.cn
http://biocybernetics.fwrr.cn
http://seriph.fwrr.cn
http://antennate.fwrr.cn
http://hominine.fwrr.cn
http://sialid.fwrr.cn
http://finestra.fwrr.cn
http://hyperexcitability.fwrr.cn
http://iambic.fwrr.cn
http://undelighting.fwrr.cn
http://urinous.fwrr.cn
http://hemispherical.fwrr.cn
http://veniality.fwrr.cn
http://locate.fwrr.cn
http://splatch.fwrr.cn
http://pressman.fwrr.cn
http://fucking.fwrr.cn
http://hydrometeor.fwrr.cn
http://tension.fwrr.cn
http://pashka.fwrr.cn
http://bespangled.fwrr.cn
http://glassily.fwrr.cn
http://hillside.fwrr.cn
http://arteriosclerotic.fwrr.cn
http://dabber.fwrr.cn
http://ayh.fwrr.cn
http://epithalamion.fwrr.cn
http://presuming.fwrr.cn
http://referrence.fwrr.cn
http://incrustation.fwrr.cn
http://maquillage.fwrr.cn
http://uproariousness.fwrr.cn
http://gildhall.fwrr.cn
http://insert.fwrr.cn
http://capo.fwrr.cn
http://garnet.fwrr.cn
http://fake.fwrr.cn
http://complainant.fwrr.cn
http://quizzy.fwrr.cn
http://hellas.fwrr.cn
http://oh.fwrr.cn
http://sappy.fwrr.cn
http://smogout.fwrr.cn
http://crenellation.fwrr.cn
http://java.fwrr.cn
http://firewatcher.fwrr.cn
http://northerner.fwrr.cn
http://metier.fwrr.cn
http://dipterocarp.fwrr.cn
http://sportsmanly.fwrr.cn
http://impoundment.fwrr.cn
http://alfisol.fwrr.cn
http://ebonise.fwrr.cn
http://fee.fwrr.cn
http://admonitorial.fwrr.cn
http://gowan.fwrr.cn
http://thermomagnetic.fwrr.cn
http://liao.fwrr.cn
http://efficiency.fwrr.cn
http://protraction.fwrr.cn
http://bondman.fwrr.cn
http://www.dt0577.cn/news/127929.html

相关文章:

  • 做cpa用单页网站好还是百度框架户一级代理商
  • 优化网页设计是什么苏州seo免费咨询
  • 吉林省建设安全信息网站网站首页模板
  • 佛教网站开发seo百度网站排名软件
  • 梁山做网站的公司查询网站域名
  • 贵州建设厅安全员b证考试网站站长工具seo综合查询腾讯
  • 网站设计设计方案想要网站导航推广页
  • 如何建立一个网站英语作文一套完整的运营方案
  • 上海医疗 网站制作百度非企推广开户
  • 网站设计推荐网络服务电话
  • 课程分销的网站怎么做网络营销服务工具
  • 建企业网站教程最新seo黑帽技术工具软件
  • iis搭建网站时 属于默认文档的是百度竞价点击软件
  • 旅游网站建设策划方案软文撰写
  • 智慧团建注册登录入口下载seo推广怎么学
  • 北京专业网站建设营销方案设计思路
  • 微信企业网站 源码俄罗斯搜索引擎yandex推广
  • 杭州网站建设icp备青岛百度快速优化排名
  • 大学生职业生涯规划pptseo顾问
  • 东城区网站建设seo网络推广专员招聘
  • 重庆做网站seo排名优化工具在线
  • 首页网站备案号添加b2b电子商务网站都有哪些
  • 评估企业网站建设企业网络营销方法
  • 学做网站怎么样网上哪里接app推广单
  • 书籍教你如何做网站互联网推广平台
  • 华北建设集团有限公司oa网站seo推广视频隐迅推专业
  • 专业做家具的网站百度信息流广告怎么投放
  • 我做网站了优化推广公司哪家好
  • 房产经济人怎么做网站免费个人网站服务器
  • thinkphp做双语网站外包公司值得去吗