高手进来看看,给点思路

已知 邮编1: String str1="343200-343299";
邮编2: String str2="343200-343219,343240";
求出 邮编1-邮编2 = 后剩下来的邮编,(结果应该是:343220-343239,343241-343299)
高手写下实现过程,效率要高点,为有的邮编范围挺大的,jdk1.4版本的,

3个回答

[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]

1,str2.split(",")得到多个条件,然后对每个条件求差集
2,每个条件在str1中的只有几种可能的情况:
a,条件不在str1的范围内
b,部分在str1的范围内,(这个又分两种情况:左交集或者右交集)
c,全部在str1的范围内
d,条件包含str1
3,针对以上情况分别处理得到结果

public class Main {

public static void main(String[] args) {
    String str1 = "343200-343299";
    String str2 = "343201-343219";
    String result = compare(str1, str2);
    System.out.println(result);
}

private static String compare(String str1, String str2) {
    String result = "";
    String[] sources = str1.split("-");
    String[] elements = str2.split("-");
    if (sources.length < 2) {
        sources = new String[] { sources[0], sources[0] };
    }
    if (elements.length < 2) {
        elements = new String[] { elements[0], elements[0] };
    }
    //elements完全在sources内部的情况,不包括边界相等
    if (sources[0].compareTo(elements[0]) < 0 && sources[1].compareTo(elements[1]) > 0) {
        result += sources[0];
        result += sources[0].equals(decString(elements[0]))?"":"-" + decString(elements[0]);
        result += ",";
        result += sources[1].equals(incString(elements[1]))?"":incString(elements[1]) + "-";
        result += sources[1];
    }
    //TODO 其他情况
    return result;
}

private static String incString(String source) {
    int lenth = source.length();
    int value = Integer.parseInt(source);
    value ++;
    String result = String.valueOf(value);
    while(result.length() < lenth) {
        result = "0" + result;
    }
    return result;
}

private static String decString(String source) {
    int lenth = source.length();
    int value = Integer.parseInt(source);
    value --;
    String result = String.valueOf(value);
    while(result.length() < lenth) {
        result = "0" + result;
    }
    return result;
}

}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐