Skip to content

Commit 4c250d2

Browse files
committed
add code
1 parent 2fcac78 commit 4c250d2

3 files changed

+42
-2
lines changed

code/123.买卖股票的最佳时机-iii.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// @lc code=start
88
class Solution {
99
public int maxProfit(int[] prices) {
10-
10+
1111
}
1212
}
1313
// @lc code=end

code/27.移除元素.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@
77
// @lc code=start
88
class Solution {
99
public int removeElement(int[] nums, int val) {
10-
10+
int l = 0;
11+
for (int i = 0; i < nums.length; i++) {
12+
if (nums[i] == val) {
13+
l++;
14+
} else {
15+
nums[i - l] = nums[i];
16+
}
17+
}
18+
return nums.length - l;
1119
}
1220
}
1321
// @lc code=end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* @lc app=leetcode.cn id=4 lang=java
3+
*
4+
* [4] 寻找两个正序数组的中位数
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
10+
return (getKth(nums1, nums2, 0, 0, (nums1.length + nums2.length + 2) / 2) + getKth(nums1, nums2, 0, 0, (nums1.length + nums2.length + 1) / 2)) * 0.5;
11+
}
12+
13+
int getKth(int[] nums1, int[] nums2, int start1, int start2, int k) {
14+
int len1 = nums1.length - start1;
15+
int len2 = nums2.length - start2;
16+
if (len1 > len2) return getKth(nums2, nums1, start2, start1, k);
17+
18+
if (len1 == 0) return nums2[start2 + k - 1];
19+
20+
if (k == 1) return Math.min(nums1[start1], nums2[start2]);
21+
22+
int i = start1 + Math.min(len1, k / 2) - 1;
23+
int j = start2 + Math.min(len2, k / 2) - 1;
24+
25+
if (nums1[i] > nums2[j]) {
26+
return getKth(nums1, nums2, start1, j + 1, k - j - 1 + start2);
27+
}
28+
return getKth(nums1, nums2, i + 1, start2, k - i - 1 + start1);
29+
}
30+
}
31+
// @lc code=end
32+

0 commit comments

Comments
 (0)