已知 邮编1: String str1="343200-343299";
邮编2: String str2="343200-343219,343240";
求出 邮编1-邮编2 = 后剩下来的邮编,(结果应该是:343220-343239,343241-343299)
高手写下实现过程,效率要高点,为有的邮编范围挺大的,jdk1.4版本的,
高手进来看看,给点思路
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- weixin_42518082 2010-05-19 22:17关注
[code="java"]
package demo;
import java.util.LinkedList;
import java.util.List;/**
-
@author yeyong
*
*/
public class ZipRangeSetTest {public static void main(String[] args) throws Exception {
// String str1 = "343200-343299";
// String str2 = "343200-343219,343240";
// String str2 = "343200-343219,343221-343290";
// String str2 = "343240,343248,343249,343299,343240";
String str1="343200-343299";
String str2="343240,343255-343260,343200-343219";
ZipRangeSet rangeSet1 = new ZipRangeSet(str1);
ZipRangeSet rangeSet2 = new ZipRangeSet(str2);
System.out.println("rangeSet1 = " + rangeSet1);
System.out.println("rangeSet2 = " + rangeSet2);
System.out.println("相减后结果 = " + rangeSet1.minus(rangeSet2));
// 输出
// rangeSet1 = [343200-343299]
// rangeSet2 = [343240, 343255-343260, 343200-343219]
// 相减后结果 = [343220-343239, 343241-343254, 343261-343299]}
}
class ZipRange {
private int start;
private int end;
private static final ZipRange[] NULL = new ZipRange[0];public ZipRange() {
}public ZipRange(int start, int end) {
this.start = start;
this.end = end;
}public boolean contains(ZipRange other) {
if (other != null) {
return other.getStart() >= start && other.getEnd() <= end;
}
return false;
}public ZipRange[] minus(ZipRange other) {
if (contains(other)) {
if (other.isSingle()) {
if (this.isSingle()) {
return NULL;
}
if (start == other.getStart()) {
return new ZipRange[] { new ZipRange(start + 1, end) };
} else if (end == other.getEnd()) {
return new ZipRange[] { new ZipRange(start, end - 1) };
} else {
return new ZipRange[] { new ZipRange(start, other.getStart() - 1),
new ZipRange(other.getStart() + 1, end) };
}
} else {
if (start == other.getStart() && end == other.getEnd()) {
return NULL;
} else if (start == other.getStart()) {
return new ZipRange[] { new ZipRange(other.getEnd() + 1, end) };
} else if (end == other.getEnd()) {
return new ZipRange[] { new ZipRange(start, other.getStart() - 1) };
} else {
return new ZipRange[] { new ZipRange(start, other.getStart() - 1),
new ZipRange(other.getEnd() + 1, end) };
}
}
} else {
return new ZipRange[] { this };
}
}public boolean isSingle() {
return start == end;
}public String toString() {
if (start == end) {
return start + "";
}return start + "-" + end;
}
public int getStart() {
return start;
}public void setStart(int start) {
this.start = start;
}public int getEnd() {
return end;
}public void setEnd(int end) {
this.end = end;
}
}class ZipRangeSet {
private List list;public ZipRangeSet(String zips) {
list = new LinkedList();
parse(zips);
}public List getList() {
return list;
}public ZipRangeSet minus(ZipRangeSet other) {
if (other == null || other.getList().size() == 0) {
return this;
}
List otherList = other.getList();
for (ZipRange otherZipRange : otherList) {
for (int i = 0, len = list.size(); i < len; i++) {
ZipRange zipRange = list.get(i);
ZipRange[] result = zipRange.minus(otherZipRange);
if (result.length == 1 && result[0] == zipRange) {
continue;
}
if (result.length == 0) {
list.remove(i);
} else {
list.set(i, result[0]);
for (int j = 1, len2 = result.length; j < len2; j++) {
list.add(i + j, result[j]);
}
}
break;
}
}
return this;
}private void parse(String zips) {
for (String str : zips.split(",")) {
String[] r = str.split("\-");
ZipRange range = new ZipRange();
range.setStart(Integer.parseInt(r[0]));
if (r.length > 1) {
range.setEnd(Integer.parseInt(r[1]));
} else {
range.setEnd(range.getStart());
}
list.add(range);
}
}@Override
public String toString() {
return list.toString();
}
}[/code]
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 -
悬赏问题
- ¥15 虚拟机打包apk出现错误
- ¥30 最小化遗憾贪心算法上界
- ¥15 用visual studi code完成html页面
- ¥15 聚类分析或者python进行数据分析
- ¥15 逻辑谓词和消解原理的运用
- ¥15 三菱伺服电机按启动按钮有使能但不动作
- ¥15 js,页面2返回页面1时定位进入的设备
- ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
- ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
- ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝