[LeetCode]题解(python):088-Merge Sorted Array
2015-12-30 14:11
627 查看
[b]题目来源:[/b]
https://leetcode.com/problems/merge-sorted-array/
[b]题意分析:[/b]
给定两个排好序的数组nums1和nums2,将两个数组整合成一个新的排好序的数组,并将这个数组存在nums1里面。
[b]题目思路:[/b]
由于题目没有要求,所以用一个tmp临时变量将nums1和nums2的数组整合起来,然后将tmp的数赋给nums1就可以了。
[b]代码(Python):[/b]
View Code
转载请注明出处:/article/6365028.html
https://leetcode.com/problems/merge-sorted-array/
[b]题意分析:[/b]
给定两个排好序的数组nums1和nums2,将两个数组整合成一个新的排好序的数组,并将这个数组存在nums1里面。
[b]题目思路:[/b]
由于题目没有要求,所以用一个tmp临时变量将nums1和nums2的数组整合起来,然后将tmp的数赋给nums1就可以了。
[b]代码(Python):[/b]
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. """ tmp = [] i,j = 0,0 while i < m or j < n: if i != m and j != n: if nums1[i] < nums2[j]: tmp.append(nums1[i]);i += 1 else: tmp.append(nums2[j]);j += 1 elif i == m: tmp.append(nums2[j]); j += 1 else: tmp.append(nums1[i]); i += 1 i = 0 while i < (m + n): nums1[i] = tmp[i] i += 1
View Code
转载请注明出处:/article/6365028.html
相关文章推荐
- Python标准库10 多进程初步 (multiprocessing包)
- 笨方法学Python(11-15)
- Use Visual studio 2010 build Python2.7.10
- [LeetCode]题解(python):086-Partition List
- [LeetCode]题解(python):083-Remove Duplicates from Sorted List
- [LeetCode]题解(python):082-Remove Duplicates from Sorted List II
- Python标准库09 当前进程信息 (os包)
- [LeetCode]题解(python):081-Search in Rotated Sorted Array II
- [LeetCode]题解(python):080-Remove Duplicates from Sorted Array II
- Python 2.1 函数的调用
- [LeetCode]题解(python):079-Word Search
- [LeetCode]题解(python):078-Subsets
- [LeetCode]题解(python):077-Combinations
- Python 第二章 函数
- [LeetCode]题解(python):076-Minimum Window Substring
- caffe 如何调用python层
- Caffe学习系列(13):数据可视化环境(python接口)配置
- python AES-16位加密解密功能实现
- Python 获取Twitter用户与Friends和Followers的关系(eg, 交集,差集)
- 仿射变换(Affine transformation)与python实践