Merge Sorted Array(easy)
来源:互联网 发布:org.apache.cxf maven 编辑:程序博客网 时间:2024/06/06 10:56
【题目】
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 tom + n) to hold additional elements from nums2. The number of elements initialized innums1 and nums2 are m and n respectively.
【题意】
两个有序数组,合并成一个有序数组,假设第一个数组空间足够容纳两个数组。
【分析】
考虑到num1数组很大,可以直接在num1数组上进行合并,但是要讲究效率。
如果单纯从前往后合并,那么效率会非常低,因为a数组后面的数字需要不停的移动。
换一种思路,我们采用从后往前合并,首先计算出总长度,设置一个指针从num1数组最后往前移动。
【题目】
public class Solution { public void merge(int A[], int m, int B[], int n) { // Start typing your Java solution below // DO NOT write main() function //input check int i=m-1; int j=n-1; int k=m+n-1; while(i>=0 && j>=0) { A[k--] = A[i] > B[j] ? A[i--] : B[j--]; } while(j>=0) { A[k--] = B[j--]; } }}
0 0
- Merge Sorted Array(easy)
- Easy 19 Merge Sorted Array(88)
- Leetcode[Easy] - Merge Sorted Array
- 【Easy】88. Merge Sorted Array
- [Leetcode 88, Easy] Merge Sorted Array
- <LeetCode><Easy> 88 Merge Sorted Array
- LeetCode-88-Merge Sorted Array(归并)-Easy
- Easy-题目39:88. Merge Sorted Array
- 88. Merge Sorted Array [easy] (Python)
- Leetcode 88. Merge Sorted Array (Easy) (cpp)
- Easy 88题 Merge Sorted Array
- Leetcode OJ 88 Merge Sorted Array [Easy]
- 【leetcode】88. Merge Sorted Array (easy)
- LeetCode-Easy刷题(18) Merge Sorted Array
- leetcode 88[easy]---Merge Sorted Array
- Leet Code OJ 88. Merge Sorted Array [Difficulty: Easy]
- 【LeetCode】88.Merge Sorted Array(easy)解题报告
- [Array]Merge Sorted Array
- 关于数据库主键和外键(终于弄懂啦)
- iOS音频处理技术-TAAE
- 运行Keras例子程序:imdb_cnn.py
- 常用资料备忘
- Git学习笔记
- Merge Sorted Array(easy)
- C#中的ASCII转换
- 盲打
- ★【16.6.2】Codeforces Round #355 (Div. 2) A. Vanya and Fence
- iOS 屏幕方向
- Python装饰器与面向切面编程
- Convert.ToInt32(Object, IFormatProvider)的含义
- for循环创建UIButton,如何去修改边框颜色
- git and github 学习笔记