寻找两个正序数组的中位数

This commit is contained in:
LingZhaoHui 2020-11-15 22:25:40 +08:00
parent 2eb67310dd
commit 528f46b646
10 changed files with 114 additions and 69 deletions

View File

@ -13,11 +13,32 @@
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@ -20,4 +20,15 @@
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
<filteredResources>
<filter>
<id>1605433362927</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>

View File

@ -0,0 +1,2 @@
eclipse.preferences.version=1
org.eclipse.jdt.apt.aptEnabled=false

View File

@ -11,5 +11,6 @@ org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.processAnnotations=disabled
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8

View File

@ -2,8 +2,8 @@
日常刷lintCode算法,提高自己的编码能力.
- [字符串相关](./src/string)
- [list相关](./src.list)
- [简单算法](./src/simple)
- [九章算法](./src/jiuzhang)
- [字符串相关](./src/main/java/com/leetcode/string)
- [list相关](./src/main/java/com/leetcode/list)
- [简单算法](./src/main/java/com/leetcode/simple)
- [九章算法](./src/main/java/com/leetcode/jiuizhang)

View File

@ -1,60 +0,0 @@
## 题目描述
给定一个包含 n 个整数的数组 nums判断 nums 中是否存在三个元素 abc 使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
注意:答案中不可以包含重复的三元组。
示例:
给定数组 nums = [-1, 0, 1, 2, -1, -4]
满足要求的三元组集合为:
```
[
[-1, 0, 1],
[-1, -1, 2]
]
```
## 解题思路
![2020-01-19_21-44.png](https://img.zeekling.cn/images/2020/01/19/2020-01-19_21-44.png)
## 答案
```java
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
int min=0,mid=1, max=nums.length-1, s= 0;
while(nums.length >= 3 && min < nums.length && nums[min] <= 0){
mid = min + 1;
max = nums.length-1;
if(min > 0 && nums[min] == nums[min -1]){
min ++;
continue;
}
while(mid < max){
s = nums[min] + nums[mid] + nums[max];
if (s < 0){
mid ++;
}else if(s > 0){
max --;
}else if (s == 0){
res.add(Arrays.asList(nums[min],nums[mid], nums[max]));
while(mid < max && nums[mid] == nums[mid + 1]){
mid ++;
}
while(mid< max && nums[max] == nums[max-1]){
max --;
}
mid ++;
max --;
}
}
min++;
}
return res;
}
}
```

View File

@ -18,7 +18,7 @@ public class PlusAB {
return a;
}
public static void main(String[] args){
public static void main(String[] args) {
PlusAB ab = new PlusAB();
System.out.println(ab.plus(1,3));
}

View File

@ -0,0 +1,70 @@
package com.leetcode.list;
import java.text.DecimalFormat;
/**
*
*
给定两个大小为 m n 的正序从小到大数组 nums1  nums2请你找出并返回这两个正序数组的中位数
进阶你能设计一个时间复杂度为 O(log (m+n)) 的算法解决此问题吗
示例 1
输入nums1 = [1,3], nums2 = [2]
输出2.00000
解释合并数组 = [1,2,3] 中位数 2
示例 2
输入nums1 = [1,2], nums2 = [3,4]
输出2.50000
解释合并数组 = [1,2,3,4] 中位数 (2 + 3) / 2 = 2.5
示例 3
输入nums1 = [0,0], nums2 = [0,0]
输出0.00000
示例 4
输入nums1 = [], nums2 = [1]
输出1.00000
示例 5
输入nums1 = [2], nums2 = []
输出2.00000
题库位置https://leetcode-cn.com/problems/median-of-two-sorted-arrays/
*
* @author zeekling
*
*/
public class FindMedianSortedArrays {
public double median(int[] nums1, int[] nums2) {
int all = nums1.length + nums2.length;
int mi = 0,ni = 0, curr = 0, pre = 0;
for ( int i = 0;i < all;i++ ) {
pre = curr;
if (mi < nums1.length && ni >= nums2.length ){
curr = nums1[mi++];
}else if (mi >= nums1.length && ni < nums2.length) {
curr = nums2[ni++];
}else {
curr = nums1[mi] < nums2[ni] ? nums1[mi++] : nums2[ni++];
}
if (i*2 == all ){
return ((double)curr + (double)pre)/2;
}
if ((i+1)*2 > all) {
return curr;
}
}
return 0;
}
public static void main(String[] args) {
FindMedianSortedArrays median = new FindMedianSortedArrays();
int[] nums1 = {2}, nums2 = {};
double res = median.median(nums1, nums2);
DecimalFormat format = new DecimalFormat("#.00000");
System.out.println(format.format(res));
}
}

View File

@ -17,9 +17,8 @@ public class PivotIndex {
for (int i=0; i< len;i++){
sum += nums[i];
}
int idx = -1;
int leftSum = 0;
for (int i=0;i<len;i++){
for (int i = 0;i < len;i ++){
if ((leftSum * 2 + nums[i]) == sum){
return i;

View File

@ -11,7 +11,8 @@ package com.leetcode.num;
public class SumNums{
public int sumNums(int n) {
boolean res = n > 0 && (n += sumNums(n-1)) > 0;
@SuppressWarnings("unused")
boolean res = n > 0 && (n += sumNums(n-1)) > 0;
return n;
}