- 浏览: 18902 次
- 性别:
- 来自: 北京
文章分类
最新评论
题目描述
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原数组长度
自己的代码
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); } }
发表评论
-
Java中String与StringBuffer的区别
2014-10-29 21:07 295String和StringBuffer的区别,网上资料可以说 ... -
String to Integer (atoi)
2014-10-29 17:13 395题目描述 Implement atoi to convert ... -
Implement strStr()
2014-10-28 15:17 279题目描述 Implement strStr(). Retu ... -
Valid Palindrome
2014-10-23 10:32 416题目描述 Given a string, determine ... -
ZigZag Conversion
2014-10-22 19:51 338题目描述 The string "PAYPALIS ... -
Add Binary
2014-10-22 19:43 298题目描述 Given two binary strings, ... -
Longest Common Prefix
2014-10-22 19:44 324题目描述 Write a function to find t ... -
Count and Say
2014-10-22 19:44 344题目描述 The count-and-say sequence ... -
Valid Sudoku
2014-10-21 10:22 352题目描述 Determine if a Sudoku is v ... -
Valid Parentheses
2014-10-21 09:41 319题目描述 Given a string containing ... -
Palindrome Number
2014-10-21 09:41 341题目描述 Determine whether an integ ... -
Length of Last Word
2014-10-21 09:41 353题目描述 Given a string s consists ... -
Minimum Depth of Binary Tree
2014-10-21 09:41 305题目描述 Given a binary tree, find ... -
Remove Nth Node From End of List
2014-10-20 16:36 252题目描述 Given a linked list, remov ... -
Path Sum
2014-10-20 15:37 290题目描述 Given a binary tree and a ... -
Binary Tree Level Order Traversal II
2014-10-20 11:17 229题目描述 Given a binary tree, retur ... -
Binary Tree Level Order Traversal
2014-10-20 11:03 289题目描述 Given a binary tree, retur ... -
Pascal's Triangle II
2014-10-20 10:07 252题目描述 Given an index k, return t ... -
Pascal's Triangle
2014-10-19 12:24 313题目描述 Given numRows, generate th ... -
Plus One
2014-10-19 11:51 331题目描述 Given a non-negative numbe ...
相关推荐
Merge Sorted Array 合并 排序 数组 leetcode
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 ...
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写题闪退 #*的多少代表此题的有意思程度 ...Merge Sorted Array 归并排序基础 Remove Duplicates from Sorted List 脑残简单题 2014.10.31 *****今天被题目ThreeSum虐出翔,打了球太累,过几天
88.合并两个有序数组 (Merge Sorted Array) 100.相同的树 (Same Tree) 104.二叉树的最大深度 (Maximum Depth of Binary Tree) 118.杨辉三角 (Pascal's Triangle) 119.杨辉三角 II (Pascal's Triangle)
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...
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 ...
leetcode 2 和 ...Array(c++) 00 94.Binary Tree Inorder Traversal(c++:tree traversal inorder) 100.Same Tree(c++) 101.对称树(c++) 104.二叉树的最大深度(c++) 108.将排序数组转换为二叉搜索树
Leetcode算法练习 Leetcode算法练习 ...MaximumSubarray 58_LengthOfLastWord 66_PlusOne 67_AddBinary 69_Sqrt(x) 70_ClimbStairs 83_RemoveDuplicatesFromSortedList 88_MergeSortedArray 100_SameT
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 ...
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<<"nonsorted_array"; print_array(array,10);... cout<<"sorted_array:"; print_array(array,10); return 0; }
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
总览 ... 例如, merge-sorted-array.py的解决方案位于https://leetcode.com/problems/merge-sorted-array/ 。 Leetcode类似问题 我发现一起解决类似的问题很有意义,这样当我们遇到新问题时,我们可以
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,
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 ...
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 ...
例如,merge-sorted-array.py 的解决方案位于 https://leetcode.com/problems/merge-sorted-array/。 Leetcode 类似问题我发现将类似问题一起解决是有意义的,这样我们在遇到新问题时可以更快地识别问题。 我的...
Leetcode经典01背包 algo 1. 数据结构与算法 数组,链表,(串和序列) 堆,栈,队列 树,图 排序,搜索 贪心,回溯,动态规划 堆:一种完全二叉树,任意节点大于左右孩子(大顶堆) 树:二叉树,DFS,BFS ...Merge So
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