Skip to content

Commit

Permalink
feat: 88. Merge Sorted Array
Browse files Browse the repository at this point in the history
  • Loading branch information
黎赵太郎 committed Nov 20, 2017
1 parent 85a061d commit 16c6abf
Show file tree
Hide file tree
Showing 107 changed files with 123 additions and 0 deletions.
Empty file modified Java/.gitignore
100644 → 100755
Empty file.
Empty file modified Java/src/AddBinary.java
100644 → 100755
Empty file.
Empty file modified Java/src/AddTwoNumbers.java
100644 → 100755
Empty file.
Empty file modified Java/src/ClimbingStairs.java
100644 → 100755
Empty file.
Empty file modified Java/src/ContainerWithMostWater.java
100644 → 100755
Empty file.
Empty file modified Java/src/CountAndSay.java
100644 → 100755
Empty file.
Empty file modified Java/src/GrayCode.java
100644 → 100755
Empty file.
Empty file modified Java/src/GroupAnagrams.java
100644 → 100755
Empty file.
Empty file modified Java/src/ImplementStrStr.java
100644 → 100755
Empty file.
Empty file modified Java/src/LengthOfLastWord.java
100644 → 100755
Empty file.
Empty file modified Java/src/LongestCommonPrefix.java
100644 → 100755
Empty file.
Empty file modified Java/src/LongestSubstringWithoutRepeatingCharacters.java
100644 → 100755
Empty file.
42 changes: 42 additions & 0 deletions Java/src/MergeSortedArray.java
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 modified Java/src/MinimumPathSum.java
100644 → 100755
Empty file.
Empty file modified Java/src/PalindromeNumber.java
100644 → 100755
Empty file.
Empty file modified Java/src/PlusOne.java
100644 → 100755
Empty file.
Empty file modified Java/src/PowXN.java
100644 → 100755
Empty file.
Empty file modified Java/src/RemoveDuplicatesFromSortedArray.java
100644 → 100755
Empty file.
Empty file modified Java/src/RemoveElement.java
100644 → 100755
Empty file.
Empty file modified Java/src/ReverseInteger.java
100644 → 100755
Empty file.
Empty file modified Java/src/RomanToInteger.java
100644 → 100755
Empty file.
Empty file modified Java/src/SameTree.java
100644 → 100755
Empty file.
Empty file modified Java/src/SearchA2DMatrix.java
100644 → 100755
Empty file.
Empty file modified Java/src/SearchForARange.java
100644 → 100755
Empty file.
Empty file modified Java/src/SearchInRotatedSortedArray.java
100644 → 100755
Empty file.
Empty file modified Java/src/SearchInsertPosition.java
100644 → 100755
Empty file.
Empty file modified Java/src/SetMatrixZeroes.java
100644 → 100755
Empty file.
Empty file modified Java/src/SpiralMatrix.java
100644 → 100755
Empty file.
Empty file modified Java/src/SpiralMatrixII.java
100644 → 100755
Empty file.
Empty file modified Java/src/SqrtX.java
100644 → 100755
Empty file.
Empty file modified Java/src/StringToIntegerAtoi.java
100644 → 100755
Empty file.
Empty file modified Java/src/TwoSum.java
100644 → 100755
Empty file.
Empty file modified Java/src/UniquePaths.java
100644 → 100755
Empty file.
Empty file modified Java/src/UniquePathsII.java
100644 → 100755
Empty file.
Empty file modified Java/src/ValidParentheses.java
100644 → 100755
Empty file.
Empty file modified Kotlin/.gitignore
100644 → 100755
Empty file.
Empty file modified Kotlin/src/AddBinary.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/AddTwoNumbers.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/ClimbingStairs.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/ContainerWithMostWater.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/CountAndSay.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/GrayCode.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/GroupAnagrams.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/ImplementStrStr.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/LengthOfLastWord.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/LongestCommonPrefix.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/LongestSubstringWithoutRepeatingCharacters.kt
100644 → 100755
Empty file.
41 changes: 41 additions & 0 deletions Kotlin/src/MergeSortedArray.kt
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 modified Kotlin/src/MinimumPathSum.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/PalindromeNumber.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/PlusOne.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/PowXN.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/RemoveDuplicatesFromSortedArray.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/RemoveElement.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/ReverseInteger.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/RomanToInteger.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/SameTree.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/SearchA2DMatrix.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/SearchForARange.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/SearchInRotatedSortedArray.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/SearchInsertPosition.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/SetMatrixZeroes.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/SpiralMatrix.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/SpiralMatrixII.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/SqrtX.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/StringToIntegerAtoi.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/TwoSum.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/UniquePaths.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/UniquePathsII.kt
100644 → 100755
Empty file.
Empty file modified Kotlin/src/ValidParentheses.kt
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified Python/.gitignore
100644 → 100755
Empty file.
Empty file modified Python/AddBinary.py
100644 → 100755
Empty file.
Empty file modified Python/AddTwoNumbers.py
100644 → 100755
Empty file.
Empty file modified Python/ClimbingStairs.py
100644 → 100755
Empty file.
Empty file modified Python/ContainerWithMostWater.py
100644 → 100755
Empty file.
Empty file modified Python/CountAndSay.py
100644 → 100755
Empty file.
Empty file modified Python/GrayCode.py
100644 → 100755
Empty file.
Empty file modified Python/GroupAnagrams.py
100644 → 100755
Empty file.
Empty file modified Python/ImplementStrStr.py
100644 → 100755
Empty file.
Empty file modified Python/LengthOfLastWord.py
100644 → 100755
Empty file.
Empty file modified Python/LongestCommonPrefix.py
100644 → 100755
Empty file.
Empty file modified Python/LongestSubstringWithoutRepeatingCharacters.py
100644 → 100755
Empty file.
39 changes: 39 additions & 0 deletions Python/MergeSortedArray.py
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 modified Python/MinimumPathSum.py
100644 → 100755
Empty file.
Empty file modified Python/PalindromeNumber.py
100644 → 100755
Empty file.
Empty file modified Python/PlusOne.py
100644 → 100755
Empty file.
Empty file modified Python/PowXN.py
100644 → 100755
Empty file.
Empty file modified Python/RemoveDuplicatesFromSortedArray.py
100644 → 100755
Empty file.
Empty file modified Python/RemoveElement.py
100644 → 100755
Empty file.
Empty file modified Python/ReverseInteger.py
100644 → 100755
Empty file.
Empty file modified Python/RomanToInteger.py
100644 → 100755
Empty file.
Empty file modified Python/SameTree.py
100644 → 100755
Empty file.
Empty file modified Python/SearchA2DMatrix.py
100644 → 100755
Empty file.
Empty file modified Python/SearchForARange.py
100644 → 100755
Empty file.
Empty file modified Python/SearchInRotatedSortedArray.py
100644 → 100755
Empty file.
Empty file modified Python/SearchInsertPosition.py
100644 → 100755
Empty file.
Empty file modified Python/SetMatrixZeroes.py
100644 → 100755
Empty file.
Empty file modified Python/SpiralMatrix.py
100644 → 100755
Empty file.
Empty file modified Python/SpiralMatrixII.py
100644 → 100755
Empty file.
Empty file modified Python/SqrtX.py
100644 → 100755
Empty file.
Empty file modified Python/StringToIntegerAtoi.py
100644 → 100755
Empty file.
Empty file modified Python/TwoSum.py
100644 → 100755
Empty file.
Empty file modified Python/UniquePaths.py
100644 → 100755
Empty file.
Empty file modified Python/UniquePathsII.py
100644 → 100755
Empty file.
Empty file modified Python/ValidParentheses.py
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ All the solutions are accepted.
| 70 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/description/) | [](Java/src/ClimbingStairs.java) | [](Kotlin/src/ClimbingStairs.kt) | [](Python/ClimbingStairs.py) | Easy |
| 73 | [Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/description/) | [](Java/src/SetMatrixZeroes.java) | [](Kotlin/src/SetMatrixZeroes.kt) | [](Python/SetMatrixZeroes.py) | Medium |
| 74 | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/description/) | [](Java/src/SearchA2DMatrix.java) | [](Kotlin/src/SearchA2DMatrix.kt) | [](Python/SearchA2DMatrix.py) | Medium |
| 88 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/description/) | [](Java/src/MergeSortedArray.java) | [](Kotlin/src/MergeSortedArray.kt) | [](Python/MergeSortedArray.py)
| 89 | [Gray Code](https://leetcode.com/problems/gray-code/description/) | [](Java/src/GrayCode.java) | [](Kotlin/src/GrayCode.kt) | [](Python/GrayCode.py) | Medium |
| 100 | [Same Tree](https://leetcode.com/problems/same-tree/description/) | [](Java/src/SameTree.java) | [](Kotlin/src/SameTree.kt) | [](Python/SameTree.py)| Easy |

Expand Down

0 comments on commit 16c6abf

Please sign in to comment.