-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMergeSortedArray.java
More file actions
111 lines (105 loc) · 4.09 KB
/
Copy pathMergeSortedArray.java
File metadata and controls
111 lines (105 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* <p>给你两个按 <strong>非递减顺序</strong> 排列的整数数组 <code>nums1</code><em> </em>和 <code>nums2</code>,另有两个整数 <code>m</code> 和 <code>n</code> ,分别表示 <code>nums1</code> 和 <code>nums2</code> 中的元素数目。</p>
*
* <p>请你 <strong>合并</strong> <code>nums2</code><em> </em>到 <code>nums1</code> 中,使合并后的数组同样按 <strong>非递减顺序</strong> 排列。</p>
*
* <p><strong>注意:</strong>最终,合并后数组不应由函数返回,而是存储在数组 <code>nums1</code> 中。为了应对这种情况,<code>nums1</code> 的初始长度为 <code>m + n</code>,其中前 <code>m</code> 个元素表示应合并的元素,后 <code>n</code> 个元素为 <code>0</code> ,应忽略。<code>nums2</code> 的长度为 <code>n</code> 。</p>
*
* <p> </p>
*
* <p><strong>示例 1:</strong></p>
*
* <pre>
* <strong>输入:</strong>nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
* <strong>输出:</strong>[1,2,2,3,5,6]
* <strong>解释:</strong>需要合并 [1,2,3] 和 [2,5,6] 。
* 合并结果是 [<em><strong>1</strong></em>,<em><strong>2</strong></em>,2,<em><strong>3</strong></em>,5,6] ,其中斜体加粗标注的为 nums1 中的元素。
* </pre>
*
* <p><strong>示例 2:</strong></p>
*
* <pre>
* <strong>输入:</strong>nums1 = [1], m = 1, nums2 = [], n = 0
* <strong>输出:</strong>[1]
* <strong>解释:</strong>需要合并 [1] 和 [] 。
* 合并结果是 [1] 。
* </pre>
*
* <p><strong>示例 3:</strong></p>
*
* <pre>
* <strong>输入:</strong>nums1 = [0], m = 0, nums2 = [1], n = 1
* <strong>输出:</strong>[1]
* <strong>解释:</strong>需要合并的数组是 [] 和 [1] 。
* 合并结果是 [1] 。
* 注意,因为 m = 0 ,所以 nums1 中没有元素。nums1 中仅存的 0 仅仅是为了确保合并结果可以顺利存放到 nums1 中。
* </pre>
*
* <p> </p>
*
* <p><strong>提示:</strong></p>
*
* <ul>
* <li><code>nums1.length == m + n</code></li>
* <li><code>nums2.length == n</code></li>
* <li><code>0 <= m, n <= 200</code></li>
* <li><code>1 <= m + n <= 200</code></li>
* <li><code>-10<sup>9</sup> <= nums1[i], nums2[j] <= 10<sup>9</sup></code></li>
* </ul>
*
* <p> </p>
*
* <p><strong>进阶:</strong>你可以设计实现一个时间复杂度为 <code>O(m + n)</code> 的算法解决此问题吗?</p>
* <div><div>Related Topics</div><div><li>数组</li><li>双指针</li><li>排序</li></div></div><br><div><li>👍 1184</li><li>👎 0</li></div>
*/
package leetcode1;
public class MergeSortedArray {
public static void main(String[] args) {
Solution0 solution = new MergeSortedArray().new Solution0();
}
/**
* 要细心,要一个一个变量检查,不要写错了
*/
class Solution0 {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int i1 = m - 1;
int i2 = n - 1;
int right = nums1.length - 1;
while (i1 >= 0 && i2 >= 0) {
if (nums1[i1] > nums2[i2]) {
nums1[right--] = nums1[i1--];
} else {
nums1[right--] = nums2[i2--];
}
}
while (i2 >= 0) {
nums1[right--] = nums2[i2--];
}
}
}
class Solution1 {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int i = m + n - 1;
m--;
n--;
while (m >= 0 && n >= 0) {
nums1[i--] = nums1[m] > nums2[n] ? nums1[m--] : nums2[n--];
}
System.arraycopy(nums2, 0, nums1, 0, n + 1);
}
}
class Solution2 {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int k = nums1.length - 1;
m--;
n--;
while (m >= 0 && n >= 0) {
if (nums1[m] > nums2[n]) nums1[k--] = nums1[m--];
else nums1[k--] = nums2[n--];
}
while (n >= 0) {
nums1[k--] = nums2[n--];
}
}
}
}