算法 计算多个顺序区间的总长

复制代码
List<Point> list = Arrays.asList(new Point(1, 5), new Point(2, 3),
new Point(2, 8), new Point(9, 13));
int sum = list.get(0).y - list.get(0).x,currenty=list.get(0).y;
Point  pnext;
for (int i = 1, len = list.size(); i < len; i++) {
pnext = list.get(i);
if (currenty >= pnext.y)
continue;
sum += currenty >= pnext.x ? pnext.y - currenty : pnext.y
- pnext.x;
currenty = pnext.y;
}
System.out.println(sum);
复制代码

 


posted @   荒土  阅读(279)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示