英文:
Delete numbers from the existing set of data and create new sets
问题
以下是翻译好的部分:
我有一个区间列表。现在我将得到一个数字列表,我必须从区间中删除这些数字,并创建新的区间。所有连续的数字应该放在一个区间内,不连续的数字应该放在不同的区间内。
public class DriverClass{
public static void main(String[] args){
List<Interval> intervals = new ArrayList<Interval>();
Interval i1 = new Interval(5, 9);
Interval i2 = new Interval(12, 23);
intervals.add(i1);
intervals.add(i2);
List<Integer> numbersToBeDeleted = Arrays.asList(6, 7, 19, 21);
}
}
删除数字 6、7、19、21 后,期望的输出为:[5,5], [8,9], [12,18], [20,20], [22,23]
class Interval {
int start, end;
Interval(int start, int end) {
this.start = start;
this.end = end;
}
}
我无法思考出解决方案,任何建议都将有所帮助!
英文:
I have List of ranges. Now I will be provided with the List of number & I have to delete the number form the ranges & create new ranges. <br/>All continous number should come in one range & discontinous number should come in seperate range.
<br/>
public class DriverClass{
public static void main(String[] args){
List<Interval> intervals = new ArrayList<Interval>();
Interval i1 = new Interval(5, 9);
Interval i2 = new Interval(12, 23);
intervals.add(i1);
intervals.add(i2);
List<Integer> numbersToBeDeleted = Arrays.asList(6, 7, 19, 21);
}
}
After deleting the 6, 7, 19, 21 <br/>
Expected output: [5,5], [8,9], [12,18], [20,20], [22,23]
class Interval {
int start, end;
Interval(int start, int end) {
this.start = start;
this.end = end;
}
}
<br/>
I am unable to think through the solution, any suggestion would be helpful!
答案1
得分: 1
对于每个要删除的数字:
- 找到包含该数字的区间的索引(如果有的话;如果没有这样的区间,操作完成,可以考虑抛出异常)。
- 根据情况:
- 如果要删除的数字是区间中唯一的数字,则删除整个区间。
- 否则,如果该数字是区间的第一个或最后一个数字,则缩短该区间。
- 否则,将区间分割成两个区间。
您可能希望以不同的方式编写条件检查的代码,但这应该是基本思路。对于从列表中删除和插入区间,List.remove(int)
和 List.add(int, E)
方法应该很有用。
附注:可能方便您在 Interval
类中添加方法,用于确定是否包含数字,是否为首或末尾数字,以及在前端或后端缩短区间的方法。甚至可能添加一个将区间分割成两个的方法。
英文:
For each number to be deleted:
- Find the index of interval containing that number (if any; if no such interval, you’re done, may consider throwing).
- Depending:
- If the number to be deleted is the only number in the interval, delete the entire interval.
- Otherwise if the number is first or last in the interval, shorten the interval.
- Otherwise split into two intervals.
You may want to code the condition checking differently, but this should be the basic idea. For deleting and inserting intervals in the list the List.remove(int)
and List.add(int, E)
methods should be useful.
PS It will probably be convenient to add methods to your Interval
class to determine whether a number is contained, whether it is first or last, and to shorten the interval at the front or the rear. Maybe even a method for splitting into two intervals.
答案2
得分: 0
我已处理以下用例:
- 当现有区间的起始和结束与现有区间相同时,从列表中移除该区间。
- 如果要删除的区间位于现有范围之间,则将现有区间分为两个不包括要删除区间的区间,并将这两个新区间添加到列表中。
- 如果要删除的区间位于现有区间的边界值上,则不创建新的起始/结束区间。
示例代码:
public void deleteIntervals() {
List<Interval> intervals = new ArrayList<Interval>();
Interval i1 = new Interval(2, 9);
Interval i2 = new Interval(12, 27);
Interval i3 = new Interval(29, 500);
intervals.add(i1);
intervals.add(i2);
intervals.add(i3);
List<Interval> deleteIntervals = new ArrayList<Interval>();
Interval d1 = new Interval(2, 7);
Interval d2 = new Interval(13, 15);
Interval d3 = new Interval(18, 20);
Interval d4 = new Interval(22, 22);
Interval d5 = new Interval(50, 500);
Interval d6 = new Interval(22, 22);
deleteIntervals.add(d1);
deleteIntervals.add(d2);
deleteIntervals.add(d3);
deleteIntervals.add(d4);
deleteIntervals.add(d5);
deleteIntervals.add(d6);
List<Interval> finalDelete = new ArrayList<Interval>();
for (int i = 0; i < deleteIntervals.size(); i++) {
for (int j = 0; j < intervals.size(); j++) {
// ...
// 代码的其余部分
// ...
}
}
intervals.stream().forEach(ele -> System.out.print("[" + ele.start + ", " + ele.end + "]" + ", "));
}
请注意,上述代码仅展示了您提供的部分代码,省略了部分实现细节。
英文:
I have handled the below use cases:
- When the start and end of the existing intervals are the same as the existing intervals, then remove the interval from the list.
- If the interval to be deleted lies in the between the existing range, then I am dividing the existing interval into 2 intervals excluding the interval to be deleted & adding the 2 new intervals in the list.
- If the interval to be deleted lies in the boundary value of the existing intervals, then I am not creating the new start/end intervals.
<br/>
<br/>
Sample code:
public void deleteIntervals(){
List<Interval> intervals = new ArrayList<Interval>();
Interval i1 = new Interval(2, 9);
Interval i2 = new Interval(12, 27);
Interval i3 = new Interval(29, 500);
intervals.add(i1);
intervals.add(i2);
intervals.add(i3);
List<Interval> deleteIntervals = new ArrayList<Interval>();
Interval d1 = new Interval(2, 7);
Interval d2 = new Interval(13, 15);
Interval d3 = new Interval(18, 20);
Interval d4 = new Interval(22, 22);
Interval d5 = new Interval(50, 500);
Interval d6 = new Interval(22, 22);
deleteIntervals.add(d1);
deleteIntervals.add(d2);
deleteIntervals.add(d3);
deleteIntervals.add(d4);
deleteIntervals.add(d5);
deleteIntervals.add(d6);
List<Interval> finalDelete = new ArrayList<Interval>();
for (int i = 0; i < deleteIntervals.size(); i++) {
for (int j = 0; j < intervals.size(); j++) {
System.out.println(
"Range to delete: " + deleteIntervals.get(i).start + ", " + deleteIntervals.get(i).end);
intervals.stream().forEach(ele -> System.out.print("[" + ele.start + ", " + ele.end + "]" + ", "));
System.out.println();
if (deleteIntervals.get(i).start == intervals.get(j).start
&& deleteIntervals.get(i).end == intervals.get(j).end) {
intervals.remove(j);
break;
}
else if (deleteIntervals.get(i).start >= intervals.get(j).start
&& deleteIntervals.get(i).end <= intervals.get(j).end) {
System.out.println("Matched: [" + intervals.get(j).start + ", " + intervals.get(j).end + "]");
int newStart = (deleteIntervals.get(i).start - intervals.get(j).start);
int newEnd = intervals.get(j).end - (deleteIntervals.get(i).end);
if (newStart > 0) {
System.out.println("Adding 1: " + "[" + intervals.get(j).start + ","
+ (intervals.get(j).start + newStart - 1) + "]");
finalDelete.add(new Interval(intervals.get(j).start, (intervals.get(j).start + newStart - 1)));
intervals.add(new Interval(intervals.get(j).start, (intervals.get(j).start + newStart - 1)));
}
if (newEnd > 0) {
System.out.println("Adding 2:" + "[" + (intervals.get(j).end - newEnd + 1) + ","
+ intervals.get(j).end + "]");
finalDelete.add(new Interval((intervals.get(j).end - newEnd + 1), intervals.get(j).end));
intervals.add(new Interval((intervals.get(j).end - newEnd + 1), intervals.get(j).end));
}
intervals.remove(j);
break;
}
}
}
intervals.stream().forEach(ele -> System.out.print("[" + ele.start + ", " + ele.end + "]" + ", "));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论