-
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
zhangyuwei
committed
Oct 18, 2019
1 parent
bd03746
commit 3ce1aea
Showing
8 changed files
with
147 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
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 @@ | ||
import java.util.Arrays; | ||
|
||
public class InsertMerge { | ||
public static int[][] insert(int[][] intervals, int[] newInterval) { | ||
int[][] result = new int[intervals.length + 1][2]; | ||
int len = 0; | ||
for(int i=0; i< intervals.length; i++){ | ||
if(newInterval[0] > intervals[i][0]) { | ||
System.arraycopy(intervals, 0, result, 0, i); | ||
if (intervals[i][1] >= newInterval[0]) { | ||
int head = Math.min(intervals[i][0], newInterval[0]); | ||
int tail = newInterval[1]; | ||
for (int j = i + 1; j < intervals.length; j++) { | ||
if (intervals[j][0] <= tail) tail = Math.max(tail, intervals[0][1]); | ||
else { | ||
result[i] = new int[]{head, tail}; | ||
System.arraycopy(intervals, j, result, i + 1, intervals.length - j); | ||
return Arrays.copyOf(result, i + 1 + intervals.length - j); | ||
} | ||
} | ||
result[i] = new int[]{head, tail}; | ||
return Arrays.copyOf(result, i + 1); | ||
} | ||
}else{ | ||
|
||
} | ||
} | ||
System.arraycopy(intervals,0,result,0,intervals.length); | ||
result[intervals.length] = newInterval; | ||
return result; | ||
} | ||
|
||
public static void main(String[] args){ | ||
int[][] intervals =new int[][]{{1,3},{6,9}}; | ||
int[] newInterval =new int[]{4,5}; | ||
int[][] result = insert(intervals, newInterval); | ||
System.out.println(Arrays.toString(result[1])); | ||
} | ||
} |
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,37 @@ | ||
//给定一个非负整数数组,你最初位于数组的第一个位置。 | ||
// 数组中的每个元素代表你在该位置可以跳跃的最大长度。 | ||
// 判断你是否能够到达最后一个位置。 | ||
// 示例 1: | ||
// 输入: [2,3,1,1,4] | ||
// 输出: true | ||
// 解释: 从位置 0 到 1 跳 1 步, 然后跳 3 步到达最后一个位置。 | ||
|
||
|
||
|
||
//自顶向下的动态规划,采用递归算法 是回溯算法的优化 | ||
//自下向上的动态规划 是先从已知条件入手 然后一步一步使已知条件靠近结果 | ||
class JumpGame | ||
{ | ||
public boolean canJump(int[] nums) { | ||
int len = nums.length - 1; | ||
int i = 0; | ||
while(i<=len){ | ||
if(i + nums[i] >= len){ | ||
return true; | ||
}else if(nums[i] == 0){ | ||
return false; //要考虑到此处3为0, 永远也走不出去的情况 | ||
} | ||
int maxStep = 0; | ||
int maxI = 0; | ||
for(int j=i+1; j<=Math.min(i+nums[i],len);j++){ | ||
int tmpStep = j-i+nums[j]; | ||
if(tmpStep >= maxStep ){ | ||
maxStep = tmpStep; | ||
maxI = j; | ||
} | ||
} | ||
i = maxI; | ||
} | ||
return false; | ||
} | ||
} |
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,31 @@ | ||
import java.lang.reflect.Array; | ||
import java.util.*; | ||
|
||
public class Merge { | ||
|
||
public class MyComparable implements Comparator<int[]> { | ||
@Override | ||
public int compare(int[] o1, int[] o2) { | ||
return Integer.compare(o1[0], o2[0]); | ||
} | ||
} | ||
|
||
public int[][] merge(int[][] intervals) { | ||
ArrayList<int[]> tmpList = new ArrayList<>(); | ||
int[][] result =new int[intervals.length][2]; | ||
if(intervals.length == 0) return result; | ||
Collections.addAll(tmpList,intervals); | ||
tmpList.sort(new MyComparable()); | ||
int i = 0; | ||
result[0] = tmpList.get(0); | ||
for(int j=1;j<tmpList.size();j++){ | ||
if(tmpList.get(j)[0] <= result[i][1]){ | ||
result[i][1] = tmpList.get(j)[1]; | ||
}else{ | ||
result[++i] = tmpList.get(j); | ||
} | ||
} | ||
return Arrays.copyOf(result, i++); | ||
} | ||
|
||
} |
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,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
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,3 @@ | ||
public class InsertSort { | ||
|
||
} |