forked from TonnyL/Windary
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
黎赵太郎
committed
Nov 20, 2017
1 parent
85a061d
commit 16c6abf
Showing
107 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import java.util.Arrays; | ||
|
||
/** | ||
* Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. | ||
* <p> | ||
* Note: | ||
* You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. | ||
* The number of elements initialized in nums1 and nums2 are m and n respectively. | ||
* <p> | ||
* Accepted. | ||
*/ | ||
public class MergeSortedArray { | ||
|
||
public void merge(int[] nums1, int m, int[] nums2, int n) { | ||
if (n == 0) return; | ||
System.arraycopy(nums2, 0, nums1, m, n); | ||
Arrays.sort(nums1, 0, m + n); | ||
// System.out.print("["); | ||
// Arrays.stream(nums1).forEach(it -> System.out.print(it + ",")); | ||
// System.out.println("]"); | ||
} | ||
|
||
public static void main(String[] args) { | ||
MergeSortedArray msa = new MergeSortedArray(); | ||
|
||
// Expected: [1] | ||
msa.merge(new int[]{1}, 1, new int[]{}, 0); | ||
// Expected: [] | ||
msa.merge(new int[]{0}, 0, new int[]{1}, 1); | ||
// Expected: [1, 2, 3, 4, 5, 6] | ||
msa.merge(new int[]{4, 5, 6, 0, 0, 0}, 3, new int[]{1, 2, 3}, 3); | ||
// Expected: [0, 0, 0, 0, 1, 2, 3, 4, -1] | ||
msa.merge(new int[]{0, 0, 0, 1, 2, 3, -1, -1, -1}, 6, new int[]{0, 4}, 2); | ||
// Expected: [0, 1, 2, 3, 3, 4, 0, 0, 0] | ||
msa.merge(new int[]{0, 1, 2, 3, 0, 0, 0, 0, 0}, 4, new int[]{3, 4, 0}, 2); | ||
// Expected: [1, 1, 2, 0] | ||
msa.merge(new int[]{1, 2, 0, 0}, 2, new int[]{1}, 1); | ||
// Expected: [1, 2, 2, 3, 5, 6] | ||
msa.merge(new int[]{1, 2, 3, 0, 0, 0}, 3, new int[]{2, 5, 6}, 3); | ||
} | ||
|
||
} |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* 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 to m + n) to hold additional elements from nums2. | ||
* The number of elements initialized in nums1 and nums2 are m and n respectively. | ||
* | ||
* Accepted. | ||
*/ | ||
class MergeSortedArray { | ||
|
||
fun merge(nums1: IntArray, m: Int, nums2: IntArray, n: Int) { | ||
if (n == 0) return | ||
System.arraycopy(nums2, 0, nums1, m, n) | ||
nums1.sort(0, m + n) | ||
// println("[${nums1.joinToString()}]") | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
fun main(args: Array<String>) { | ||
val msa = MergeSortedArray() | ||
|
||
// Expected: [1] | ||
msa.merge(intArrayOf(1), 1, intArrayOf(), 0) | ||
// Expected: [] | ||
msa.merge(intArrayOf(0), 0, intArrayOf(1), 1) | ||
// Expected: [1, 2, 3, 4, 5, 6] | ||
msa.merge(intArrayOf(4, 5, 6, 0, 0, 0), 3, intArrayOf(1, 2, 3), 3) | ||
// Expected: [0, 0, 0, 0, 1, 2, 3, 4, -1] | ||
msa.merge(intArrayOf(0, 0, 0, 1, 2, 3, -1, -1, -1), 6, intArrayOf(0, 4), 2) | ||
// Expected: [0, 1, 2, 3, 3, 4, 0, 0, 0] | ||
msa.merge(intArrayOf(0, 1, 2, 3, 0, 0, 0, 0, 0), 4, intArrayOf(3, 4, 0), 2) | ||
// Expected: [1, 1, 2, 0] | ||
msa.merge(intArrayOf(1, 2, 0, 0), 2, intArrayOf(1), 1) | ||
// Expected: [1, 2, 2, 3, 5, 6] | ||
msa.merge(intArrayOf(1, 2, 3, 0, 0, 0), 3, intArrayOf(2, 5, 6), 3) | ||
} | ||
} | ||
|
||
} |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# -*- coding: UTF-8 -*- | ||
# | ||
# 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 to m + n) to hold additional elements from nums2. | ||
# The number of elements initialized in nums1 and nums2 are m and n respectively. | ||
# | ||
# Python, Python 3 accepted. | ||
class MergeSortedArray: | ||
def merge(self, nums1, m, nums2, n): | ||
""" | ||
:type nums1: List[int] | ||
:type m: int | ||
:type nums2: List[int] | ||
:type n: int | ||
:rtype: void Do not return anything, modify nums1 in-place instead. | ||
""" | ||
if n != 0: | ||
nums1[m:m + n] = nums2 | ||
nums1[0:m + n] = sorted(nums1[0:m + n]) | ||
# print(nums1) | ||
|
||
|
||
msa = MergeSortedArray() | ||
# Expected: [1] | ||
msa.merge([1], 1, [], 0) | ||
# Expected: [] | ||
msa.merge([0], 0, [1], 1) | ||
# Expected: [1, 2, 3, 4, 5, 6] | ||
msa.merge([4, 5, 6, 0, 0, 0], 3, [1, 2, 3], 3) | ||
# Expected: [0, 0, 0, 0, 1, 2, 3, 4, -1] | ||
msa.merge([0, 0, 0, 1, 2, 3, -1, -1, -1], 6, [0, 4], 2) | ||
# Expected: [0, 1, 2, 3, 3, 4, 0, 0, 0] | ||
msa.merge([0, 1, 2, 3, 0, 0, 0, 0, 0], 4, [3, 4, 0], 2) | ||
# Expected: [1, 1, 2, 0] | ||
msa.merge([1, 2, 0, 0], 2, [1], 1) | ||
# Expected: [1, 2, 2, 3, 5, 6] | ||
msa.merge([1, 2, 3, 0, 0, 0], 3, [2, 5, 6], 3) |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters