从现有数据集中删除数字并创建新的数据集。

huangapple go评论109阅读模式
英文:

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&lt;Interval&gt; intervals = new ArrayList&lt;Interval&gt;();
	 Interval i1 = new Interval(5, 9);
	 Interval i2 = new Interval(12, 23);
	 intervals.add(i1);
	 intervals.add(i2);
 
     List&lt;Integer&gt; 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

对于每个要删除的数字:

  1. 找到包含该数字的区间的索引(如果有的话;如果没有这样的区间,操作完成,可以考虑抛出异常)。
  2. 根据情况:
    1. 如果要删除的数字是区间中唯一的数字,则删除整个区间。
    2. 否则,如果该数字是区间的第一个或最后一个数字,则缩短该区间。
    3. 否则,将区间分割成两个区间。

您可能希望以不同的方式编写条件检查的代码,但这应该是基本思路。对于从列表中删除和插入区间,List.remove(int)List.add(int, E) 方法应该很有用。

附注:可能方便您在 Interval 类中添加方法,用于确定是否包含数字,是否为首或末尾数字,以及在前端或后端缩短区间的方法。甚至可能添加一个将区间分割成两个的方法。

英文:

For each number to be deleted:

  1. Find the index of interval containing that number (if any; if no such interval, you’re done, may consider throwing).
  2. Depending:
    1. If the number to be deleted is the only number in the interval, delete the entire interval.
    2. Otherwise if the number is first or last in the interval, shorten the interval.
    3. 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

我已处理以下用例:

  1. 当现有区间的起始和结束与现有区间相同时,从列表中移除该区间。
  2. 如果要删除的区间位于现有范围之间,则将现有区间分为两个不包括要删除区间的区间,并将这两个新区间添加到列表中。
  3. 如果要删除的区间位于现有区间的边界值上,则不创建新的起始/结束区间。

示例代码:

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:

  1. When the start and end of the existing intervals are the same as the existing intervals, then remove the interval from the list.
  2. 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.
  3. 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&lt;Interval&gt; intervals = new ArrayList&lt;Interval&gt;();
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&lt;Interval&gt; deleteIntervals = new ArrayList&lt;Interval&gt;();
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&lt;Interval&gt; finalDelete = new ArrayList&lt;Interval&gt;();
for (int i = 0; i &lt; deleteIntervals.size(); i++) {
for (int j = 0; j &lt; intervals.size(); j++) {
System.out.println(
&quot;Range to delete: &quot; + deleteIntervals.get(i).start + &quot;, &quot; + deleteIntervals.get(i).end);
intervals.stream().forEach(ele -&gt; System.out.print(&quot;[&quot; + ele.start + &quot;, &quot; + ele.end + &quot;]&quot; + &quot;, &quot;));
System.out.println();
if (deleteIntervals.get(i).start == intervals.get(j).start
&amp;&amp; deleteIntervals.get(i).end == intervals.get(j).end) {
intervals.remove(j);
break;
}
else if (deleteIntervals.get(i).start &gt;= intervals.get(j).start
&amp;&amp; deleteIntervals.get(i).end &lt;= intervals.get(j).end) {
System.out.println(&quot;Matched: [&quot; + intervals.get(j).start + &quot;, &quot; + intervals.get(j).end + &quot;]&quot;);
int newStart = (deleteIntervals.get(i).start - intervals.get(j).start);
int newEnd = intervals.get(j).end - (deleteIntervals.get(i).end);
if (newStart &gt; 0) {
System.out.println(&quot;Adding 1: &quot; + &quot;[&quot; + intervals.get(j).start + &quot;,&quot;
+ (intervals.get(j).start + newStart - 1) + &quot;]&quot;);
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 &gt; 0) {
System.out.println(&quot;Adding 2:&quot; + &quot;[&quot; + (intervals.get(j).end - newEnd + 1) + &quot;,&quot;
+ intervals.get(j).end + &quot;]&quot;);
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 -&gt; System.out.print(&quot;[&quot; + ele.start + &quot;, &quot; + ele.end + &quot;]&quot; + &quot;, &quot;));
}

huangapple
  • 本文由 发表于 2020年9月5日 16:20:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63751865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定