【LeetCode從零單排】No88.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 nrespectively.
程式碼
public class Solution {
public void merge(int A[], int m, int B[], int n) {
while (m > 0 && n > 0) {
if (A[m-1] > B[n-1]) {
A[m+n-1] = A[m-1];
m--;
} else {
A[m+n-1] = B[n-1];
n--;
}
}
while (n > 0) {
A[n-1] = B[n-1];
n--;
}
}
}
/********************************
* 本文來自部落格 “李博Garvin“
* 轉載請標明出處:http://blog.csdn.net/buptgshengod
******************************************/
相關文章
- 【LeetCode從零單排】No26.Remove Duplicates from Sorted ArrayLeetCodeREM
- 【LeetCode從零單排】No83 Remove Duplicates from Sorted ListLeetCodeREM
- 【LeetCode從零單排】No189 .Rotate ArrayLeetCode
- 【LeetCode從零單排】No38.CountAndSayLeetCode
- Leetcode Search in Rotated Sorted ArrayLeetCode
- Leetcode Merge Sorted ArrayLeetCode
- 【LeetCode從零單排】No.7 Reverse IntegerLeetCode
- 【LeetCode從零單排】No20.ValidParenthesesLeetCode
- 【LeetCode從零單排】No19.RemoveNthNodeFromEndofListLeetCodeREM
- 【LeetCode從零單排】No21.MergeTwoSortedListsLeetCode
- 【LeetCode從零單排】No27.Remove ElementLeetCodeREM
- 【LeetCode從零單排】No28 Implement strStr()LeetCode
- 【LeetCode從零單排】No22.Generate ParenthesesLeetCode
- 【LeetCode從零單排】No58.Length of Last WordLeetCodeAST
- 【LeetCode從零單排】No67.AddBinaryLeetCode
- 【LeetCode從零單排】No70.ClimbingStairsLeetCodeAI
- 【LeetCode從零單排】No.9 Palindrome NumberLeetCode
- 【LeetCode從零單排】No14.LongestCommonPrefixLeetCode
- 【LeetCode從零單排】No36 Valid SudokuLeetCode
- 【LeetCode從零單排】No221.Maximal SquareLeetCode
- Leetcode 33 Search in Rotated Sorted ArrayLeetCode
- Leetcode Search in Rotated Sorted Array IILeetCode
- leetcode Remove Duplicates from Sorted ArrayLeetCodeREM
- Leetcode-Merge Sorted ArrayLeetCode
- Leetcode-Search in Rotated Sorted ArrayLeetCode
- Search in Rotated Sorted Array leetcode javaLeetCodeJava
- 【LeetCode從零單排】No15 3SumLeetCode
- Mysql從零單排-1MySql
- LeetCode之Squares of a Sorted Array(Kotlin)LeetCodeKotlin
- Leetcode 26 Remove Duplicates from Sorted ArrayLeetCodeREM
- Leetcode 88. Merge Sorted ArrayLeetCode
- leetcode33_Search in Rotated Sorted ArrayLeetCode
- LeetCode-Search in Rotated Sorted Array IILeetCode
- Leetcode Remove Duplicates from Sorted Array IILeetCodeREM
- Leetcode-Convert Sorted Array to BSTLeetCode
- Leetcode-Remove Duplicates from Sorted ArrayLeetCodeREM
- Median of Two Sorted Array leetcode javaLeetCodeJava
- Remove Duplicates from Sorted Array leetcode javaREMLeetCodeJava