例如:
int[][] a=
{
{1,2,3,4,5},
{1,2,3,4,6},
{1,2,3,4,7}
};
行合并的规则为:两行中只相差一个数字,就把相差的数字进行合并,例如将a进行行合并处理后,变为:
int[][] b=
{
{1,2,3,4,11},
{1,2,3,4,7}
};
再对 b进行行合并后,变为:
int[][] c=
{
{1,2,3,4,18}
};
请用java写出能实现合并规则,行数最多可能为5万。
例如:
int[][] a=
{
{1,2,3,4,5},
{1,2,3,4,6},
{1,2,3,4,7}
};
行合并的规则为:两行中只相差一个数字,就把相差的数字进行合并,例如将a进行行合并处理后,变为:
int[][] b=
{
{1,2,3,4,11},
{1,2,3,4,7}
};
再对 b进行行合并后,变为:
int[][] c=
{
{1,2,3,4,18}
};
请用java写出能实现合并规则,行数最多可能为5万。
试写一个:
[code="java"]
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
public class CombinArray {
public static ArrayList combine(ArrObject theArr,
ArrObject otherArr) {
List one = theArr.getArr();
List other = otherArr.getArr();
ArrayList combined = new ArrayList();
int noEqualCount = 0;
int equalCount = 0;
if (one.size() == other.size()) {
for (int i = 0; i < one.size(); i++) {
if (one.get(i) - other.get(i) != 0) {
noEqualCount++;
combined.add(one.get(i) + other.get(i));
} else {
equalCount++;
combined.add(one.get(i));
}
}
}
// Equal and have only one difference can combine to one.
if (noEqualCount == 1 || equalCount == one.size()) {
System.out.println("middle:" + combined);
return combined;
}
return null;
}
@Test
public void testCombine() {
ArrObject arr1 = new ArrObject(Arrays.asList(1, 2, 3));
ArrObject arr2 = new ArrObject(Arrays.asList(1, 2, 3));
ArrObject arr3 = new ArrObject(Arrays.asList(1, 2, 8));
ArrObject arr4 = new ArrObject(Arrays.asList(1, 3, 8));
ArrObject arr5 = new ArrObject(Arrays.asList(1, 3, 8));
ArrObject arr6 = new ArrObject(Arrays.asList(3, 8, 1));
ArrayList<ArrObject> arrayList = new ArrayList<ArrObject>(
Arrays.asList(arr1, arr2, arr3, arr4, arr5, arr6));
ArrayList<ArrObject> resultList = new ArrayList<ArrObject>();
for (int i = 0; i < arrayList.size(); i++) {
ArrObject one = arrayList.get(i);
ArrObject other = arrayList.get((i + 1) / arrayList.size());
ArrayList<Integer> result = combine(one, other);
if (result != null && !resultList.contains(result)) {
arrayList.add(new ArrObject(result));
arrayList.remove(one);
arrayList.remove(other);
resultList.add(new ArrObject(result));
}
print(arrayList);
}
System.out.println("==========result==========");
print(resultList);
}
private void print(ArrayList<ArrObject> copyofList) {
System.out.println("finished:");
for (ArrObject arrObject : copyofList) {
System.out.println(arrObject.getArr());
}
}
}
class ArrObject {
private List arr;
private boolean combine;
public ArrObject(List<Integer> arr) {
super();
Collections.sort(arr);
this.arr = arr;
}
public boolean isCombine() {
return combine;
}
public void setCombine(boolean combine) {
this.combine = combine;
}
public List<Integer> getArr() {
return arr;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((arr == null) ? 0 : arr.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ArrObject other = (ArrObject) obj;
if (arr == null) {
if (other.arr != null)
return false;
} else if (!arr.equals(other.arr))
return false;
return true;
}
}
[/code]