LeetCode--088--合并两个有序数组
2024-09-18 17:24:33
方法1:
class Solution(object):
def merge(self, nums1, m, nums2, n):
"""
:type nums1: List[int]
:type m: int
:type nums2: List[int]
:type n: int
:rtype: void Do not return anything, modify nums1 in-place instead.
"""
while m>0 and n >0:
if nums1[m-1] >= nums2[n-1]:
nums1[m+n-1] = nums1[m-1]
m -= 1
else:
nums1[m+n-1] = nums2[n-1]
n -= 1
if n > 0:
nums1[:n] = nums2[:n]
同上:
class Solution:
def merge(self, nums1, m, nums2, n):
"""
:type nums1: List[int]
:type m: int
:type nums2: List[int]
:type n: int
:rtype: void Do not return anything, modify nums1 in-place instead.
"""
end = m + n -1
m -= 1
n -= 1
while end >= 0 and m >= 0 and n >= 0:
if nums1[m] > nums2[n]:
nums1[end] = nums1[m]
m -= 1
else:
nums1[end] = nums2[n]
n -= 1
end -= 1
while n >= 0:
nums1[end] = nums2[n]
n -= 1
end -= 1
方法3:
class Solution(object):
def merge(self, nums1, m, nums2, n):
"""
:type nums1: List[int]
:type m: int
:type nums2: List[int]
:type n: int
:rtype: void Do not return anything, modify nums1 in-place instead.
"""
nums1[m: m + n] = nums2[: n]
nums1.sort()
最新文章
- 给WinForm窗体添加快捷键 响应回车键、空格键
- SDK,API,DLL名词解释
- 通过3个Hello World应用来了解ASP.NET 5应用是如何运行的(1)
- 6-tips-for-managing-property-files-with-spring--转
- Codeforces Round #303 (Div. 2) E. Paths and Trees 最短路+贪心
- [转贴] C/C++中动态链接库的创建和调用
- 删除WIN7系统的共享文件
- js变量作用域及访问权限的探讨(2)
- [bzoj1783] [Usaco2010 Jan]Taking Turns
- codeforces 600E . Lomsat gelral (线段树合并)
- Confluence 6 白名单表达式类型
- UVA12171-Sculpture(离散化+floodfill)
- 利用ant脚本 自动构建svn增量/全量 系统程序升级包【转】
- javascript 高级程序设计 九
- jbosscache
- python---补充locals()变量在变量分发中的使用
- Tomcat 多项目管理
- swapon和swapoff命令详解
- go_接口
- postman 上一个接口返回值传给下一个接口
热门文章
- Intermediate Python for Data Science learning 3 - Customization
- jsp/servlet/mysql/linux基本概念和操作
- SQL学习之MYSQL的常用命令和增删改查语句和数据类型
- linux 挂载硬盘 + 对硬盘 分区
- Thinkphp5 引入第三方类库的方法
- 20145122 《Java程序设计》课程总结
- 《网络攻防》实验九:web安全基础实践
- ms08_067攻击实验
- JAVA I/O(一)基本字节和字符IO流
- Package libvirt was not found in the pkg-config search path