【leetcode】88. Merge Sorted Array(Python & C++)
2017-09-05 21:39
621 查看
88. Merge Sorted Array
题目链接88.1 题目描述:
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:
You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.
88.2 解题思路:
思路一:(仅C++)利用迭代器,同时遍历两个数组,如果遇到数组2的值比较小,则将其插入数组1中。然后迭代器++。出现问题是题意理解的不对,数组1中并不是所有元素都参与排序,而是取决于m,m决定了数组1排序的个数,所以排序完,只要数组1中前m+n个有序即可。思路二:分为C++和Python。
C++:重新创建一个数组,将数组1中前m个元素和数组2排序完放在新数组中,然后将新数组赋值给数组1。
python:数组1从m个元素往前比较,数组2从后往前比较。当两者都没到头时,根据大小从m+n-1出往前更新数组1。循环结束,如果数组2没有到头,将数组2中剩余元素同位置赋给数组1即可。
思路三:与思路二中Python的思想差不多。设置遍历数组1和数组2的指针i=m-1,j=n-1,设置更新数组1的指针k=m+n-1。遍历数组,如果数组1中的值大于数组2,或者数组2遍历到头了,并且数组1没有到头,则更新数组1,nums1[k] = nums1[i–],否则nums1[k] = nums2[j–]。当k小于0时遍历结束。
88.3 C++代码:
1、思路一代码(失败):class Solution114_0 { public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { //失败,原因是数组1空间比m+n大,可能大很多,但初始化了前m个元素, //也就是说,只比较数组1前m个 if (m == 0) nums1 = nums2; else { vector<int>::iterator i = nums1.begin(); vector<int>::iterator j = nums2.begin(); while (i != nums1.begin() && j != nums2.end()) { if (*i > *j) { i = nums1.insert(i, *j); j++; } i++; } } } };
2、思路二代码(3ms):
class Solution114 { public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { vector<int>nums; int i = 0, j = 0; while (i<m || j<n) { if (i<m && j<n) { if (nums1[i]<nums2[j]) { nums.push_back(nums1[i]); i++; } else { nums.push_back(nums2[j]); j++; } } else if (i<m) { nums.push_back(nums1[i]); i++; } else if (j<n) { nums.push_back(nums2[j]); j++; } } nums1 = nums; } };
3、思路三代码(6ms)
class Solution114_1 { public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int k; int i = m - 1, j = n - 1; for (k= m+n-1; k >= 0;k--) { if ((j < 0 || nums1[i] > nums2[j]) && i >= 0) nums1[k] = nums1[i--]; else nums1[k] = nums2[j--]; } } };
88.4 Python代码:
1、思路二代码(45ms)class Solution1(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[n+m-1]=nums1[m-1] m-=1 else: nums1[n+m-1]=nums2[n-1] n-=1 if n>0: nums1[:n]=nums2[:n]
2、思路三代码(39ms)
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. """ i=m-1 j=n-1 k=m+n-1 while k>=0: if (j<0 or nums1[i]>nums2[j]) and i>=0: nums1[k]=nums1[i] i-=1 else: nums1[k]=nums2[j] j-=1 k-=1
相关文章推荐
- LeetCode88. Merge Sorted Array-python(easy)
- [LeetCode]88. Merge Sorted Array
- 【LeetCode】Merge Two Sorted Lists 解题报告(Java & Python)
- leetcode Valid Palindrome C++&python 题解
- leetcode 88. Merge Sorted Array
- 【LeetCode with Python】 Pascal's Triangle II
- leetcode_88. Merge Sorted Array
- 【LeetCode】Validate Binary Search Tree 解题报告(Java & Python)
- [LeetCode]88. Merge Sorted Array
- Leetcode - 88. Merge Sorted Array
- 【leetcode】219. Contains Duplicate II(Python & C++)
- 【leetcode】125. Valid Palindrome(Python & C++)
- leetcode 88. Merge Sorted Array
- LeetCode 88. Merge Sorted Array
- 【leetcode】189. Rotate Array(Python & C++)
- 【leetcode】58. Length of Last Word(Python & C++)
- [leetcode]88. Merge Sorted Array
- leetcode 解题 String to Integer (atoi)(C&python)
- 【leetcode】479. Largest Palindrome Product(Python & C++)
- Leetcode 88. Merge Sorted Array