`
淡淡的一抹
  • 浏览: 18902 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Merge Sorted Array

 
阅读更多
题目描述
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 (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.

解题思路
本题根据题目提示,想到了用插入排序来解决。由于两个数组都排好序了,因此在使用插入排序时,运用了一个标记来优化。

相关知识
(1)将两个数组合并
System.arraycopy(src, srcPos, des, desPos, srcLen);
src:原数组
srcPos原数组开始下标
des目的数组
desPos目的数组开始下标
srcLen原数组长度


自己的代码
package leetcode;

import java.util.Arrays;

public class MergeSortedArray {
	public void merge(int A[], int m, int B[], int n) {
        System.arraycopy(B, 0, A, m, n);
        
        //利用变形的插入排序进行合并
        int index = 0;
        for(int i = m; i < m + n; i++){
        	for(int j = index; j < i; j++){
        		if(A[i] < A[j]) {
        			int sym = A[i];
        			for(int k = i; k >j; k--) 
        				A[k] = A[k-1];
        			A[j] = sym;
        			index = j;
        		}
        	}
        }
        
        //System.out.println(Arrays.toString(A));
        
    }
	
	public static void main(String[] args) {
		int[] A = new int[9];
		A[0] = 1;
		A[1] = 3;
		A[2] = 3;
		A[3] = 6;
		A[4] = 7;
		int[] B = {1, 1, 2, 8};
		
		MergeSortedArray msa = new MergeSortedArray();
		msa.merge(A, 5, B, B.length);
	}
}
分享到:
评论

相关推荐

    Merge Sorted Array合并排序数组leetcode

    Merge Sorted Array 合并 排序 数组 leetcode

    Coding Interview In Java

    20 Merge Sorted Array 61 ... ... 231 Counting Bits 561 232 Maximum Product of Word Lengths 563 233 Gray Code 565 234 Permutations 567 235 Permutations II 571 236 Permutation Sequence 573 237 Generate ...

    LeetCode C++全解

    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 ...

    leetcode写题闪退-LeetCode:leetcodeOJ

    leetcode写题闪退 #*的多少代表此题的有意思程度 ...Merge Sorted Array 归并排序基础 Remove Duplicates from Sorted List 脑残简单题 2014.10.31 *****今天被题目ThreeSum虐出翔,打了球太累,过几天

    leetcode-js:算法和数据结构是一个程序员的灵魂,LeetCode JavaScript TypeScript 题解

    88.合并两个有序数组 (Merge Sorted Array) 100.相同的树 (Same Tree) 104.二叉树的最大深度 (Maximum Depth of Binary Tree) 118.杨辉三角 (Pascal's Triangle) 119.杨辉三角 II (Pascal's Triangle)

    LeetCode最全代码

    26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [C++](./C++/remove-duplicates-from-sorted-array.cpp) [Python](./Python/remove-duplicates...

    leetcode添加元素使和等于-LeetCodeNotes:力码笔记

    Merge Sorted Array 描述:合并两个有序数组,将B合并入A,A长度刚好为A.length + B.length nums1 = [1,2,3,0,0,0], m = 3 nums2 = [2,5,6], n = 3 思路:从后往前将B并入A可以避免使用额外空间 121. Best Time to ...

    leetcode2sumc-Leetcode_questions:Leetcode_questions

    leetcode 2 和 ...Array(c++) 00 94.Binary Tree Inorder Traversal(c++:tree traversal inorder) 100.Same Tree(c++) 101.对称树(c++) 104.二叉树的最大深度(c++) 108.将排序数组转换为二叉搜索树

    Leetcode-Algorithm-Exercise

    Leetcode算法练习 Leetcode算法练习 ...MaximumSubarray 58_LengthOfLastWord 66_PlusOne 67_AddBinary 69_Sqrt(x) 70_ClimbStairs 83_RemoveDuplicatesFromSortedList 88_MergeSortedArray 100_SameT

    cpp-算法精粹

    Remove Duplicates from Sorted Array II Longest Consecutive Sequence Two Sum 3Sum 3Sum Closest 4Sum Remove Element Move Zeroes Next Permutation Permutation Sequence Valid Sudoku Trapping Rain Water ...

    Simple Linear Work Suffix Array Construction

    3. merge the two sorted sequences obtained in steps one and two. The algorithm is much simpler than previous linear time algorithms that are all based on the more complicated suffix tree data ...

    各种排序算法

    int _tmain(int argc, _TCHAR* argv[]) { int array[10]={10,3,5,2,4,1,8,7,9,6}; cout&lt;&lt;"nonsorted_array"; print_array(array,10);... cout&lt;&lt;"sorted_array:"; print_array(array,10); return 0; }

    lrucacheleetcode-leetcode:leetcode

    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-python:Leetcode Python解决方案和解释。 也是准备软件工程师面试的指南

    总览 ... 例如, merge-sorted-array.py的解决方案位于https://leetcode.com/problems/merge-sorted-array/ 。 Leetcode类似问题 我发现一起解决类似的问题很有意义,这样当我们遇到新问题时,我们可以

    lrucacheleetcode-geekbang-algorithms:geekbang-算法

    lru cache leetcode Class 1 ...Leetcode(https://leetcode.com/problems/merge-sorted-array) Given two sorted arrays nums1 and nums2, the goal is to merge them into a single sorted array,

    leetcode答案-LeetCode:Swift中的LeetCode

    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 ...

    leetcode-liwang:leetcode学习笔记

    space and O(n) time.Using Regular match, the complexity depends on the regular algorithm.021 Merge Two Sorted ListsTraverse the linked list. O(m+n) time, O(m+n) sapce.*0026 Remove Duplicates from ...

    Leetcode Python解决方案和解释,也是准备软件工程师面试的指南。-python

    例如,merge-sorted-array.py 的解决方案位于 https://leetcode.com/problems/merge-sorted-array/。 Leetcode 类似问题我发现将类似问题一起解决是有意义的,这样我们在遇到新问题时可以更快地识别问题。 我的...

    Leetcode经典01背包-algo:一些记录

    Leetcode经典01背包 algo 1. 数据结构与算法 数组,链表,(串和序列) 堆,栈,队列 树,图 排序,搜索 贪心,回溯,动态规划 堆:一种完全二叉树,任意节点大于左右孩子(大顶堆) 树:二叉树,DFS,BFS ...Merge So

    javalruleetcode-LeetCode::lollipop:个人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

Global site tag (gtag.js) - Google Analytics