题目描述:
Given two sorted integer arrays A and B, merge B into A as one sorted array.
Note:
You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.
新建一个数组,将原来两个数组从小到大加入这个数组中,最后再复制到A数组里。其实可以有更省空间和时间的方法。从将A和B中的数从大到小添加到A数组的末尾,不会在使用前损坏A里的前m个数,同时也不用多开一个数组。
class Solution { public: void merge(int A[], int m, int B[], int n) { int C[m+n]; int i; int point_A = 0, point_B = 0; for(i = 0;i < m + n ;i ++) { if(point_A == m) { C[i] = B[point_B++]; } else if(point_B == n) { C[i] = A[point_A++]; } else if(A[point_A] > B[point_B]) { C[i] = B[point_B++]; } else { C[i] = A[point_A++]; } } for(i = 0;i < m + n ;i ++) { A[i] = C[i]; } } };
相关推荐
Merge Sorted Array 合并 排序 数组 leetcode
java lru leetcode :ice_cream: LeetCode Kindem 的个人 LeetCode 题解仓库,欢迎交流学习。...Array 48 Rotate Image 53 Maximum Subarray 55 Jump Game 56 Merge Intervals 64 Minimum Path Sum 73
leetcode写题闪退 #*的多少代表此题的有意思程度 有几题第一次写的时候思绪比较混乱: *****Regular Expression Matching 2014.10.29 对于Find Minimum in Rotated Sorted Array II 和 Find Minimum in Rotated ...
python python_leetcode题解之088_Merge_Sorted_Array
java入门 java_leetcode题解之088_Merge_Sorted_Array
Merge Two Sorted Lists Easy #26 Remove Duplicates from Sorted Array Easy #27 Remove Element Easy #35 Search Insert Position Easy #38 Count and Say Easy #53 Maximum Subarray Easy #66 Plus One Easy #70 ...
javascript js_leetcode题解之88-merge-sorted-array.js
c语言基础 c语言_leetcode题解之0088_merge_sorted_array.zip
lru缓存leetcode leetcode ...Merge Two Sorted Lists 22. Generate Parentheses 25. Reverse Nodes in k-Group 26. Remove Duplicates from Sorted Array 27. Remove Element 28. Implement strStr() 3
leetcode 跳跃 leetcode 动态规划,背包问题 01背包问题:416. Partition Equal Subset Sum ...Array 链表 有序链表合并:21. Merge Two Sorted Lists 回文 双指针判断回文:680. 验证回文字符串 Ⅱ
merge 2 sorted array into a 3rd empty array. ##2019-03-28 斐波那契数列(Fibonacci sequence), 又称黄金分割数列、因数学家列昂纳多·斐波那契(Leonardoda Fibonacci) 以兔子繁殖为例子而引入,故又称为...
merge B into A as one sorted array.Note: You may assume that A has enough space (size that is greater or equal to m + n)to hold additional elements from B. The number of elements initialized in A and ...
leetcode中文版 LeetCode # Title Chinese Tag Solution 1 Two Sum 两数之和 array,hash 2 Add Two Numbers 两数相加 math ...Sorted ...pointers,array ...Merge Two Sorted Lists 合并两个有序链表 lin
leetcode 跳跃 leetcode 按题型标签,记录...合并K个排序链表](./Heap/merge-k-sorted-lists.md) String 字符串 [0006 Z字形变换](./String/zigzag-conversion.md) [0030 串联所有单词的子串](./String/substring
Array(数组) ID Difficulty Title Java Python 1 Easy 两数之和 26 Easy 删除排序数组中的重复项 27 Easy 移除元素 35 Easy 搜索插入位置 Linked List(链表) ID Difficulty Title Java Python 21 Easy Merge Two ...
Merge_Sorted_Array 简单的 大批 125 有效回文 简单的 大批 167 两个 Sum II Input 数组排序 简单的 大批 209 最小尺寸子阵列总和 中等的 大批 215 数组中的第 K 个最大元素 中等的 大批 217 包含_重复 简单的 大批 ...
Merge Sorted Array vi. Sum vii. Find Minimum in Rotated Sorted Array viii. Largest Rectangle in Histogram ix. Maximal Rectangle x. Palindrome Number xi. Search a 2D Matrix xii. Search for a Range ...
- Merge Sorted Array II(合并两个有序数组II) - Median(中位数) - Partition Array by Odd and Even(奇偶分割数组) - **Binary Search**(二分查找) - Kth Largest Element(第k个最大元素) - First ...
- **Merge Sorted Array**:合并两个已排序的数组,使合并后的数组仍然有序。 - **Sum**:计算数组的总和。 - **Find Minimum in Rotated Sorted Array**:在一个旋转了的有序数组中找到最小值。 - **Largest ...
* Merge Sorted Array:该题目要求合并两个排序数组,实现方法使用了迭代算法。 * Valid Parentheses:该题目要求检查括号是否匹配,实现方法使用了栈的数据结构。 * Implement strStr():该题目要求实现字符串查找...