问题描述
例如: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万。 问题补充:jobar 写道
解决方案
试写一个: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<Integer> combine(ArrObject theArr,ArrObject otherArr) {List<Integer> one = theArr.getArr();List<Integer> other = otherArr.getArr();ArrayList<Integer> combined = new ArrayList<Integer>();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;}@Testpublic 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<Integer> 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;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + ((arr == null) ? 0 : arr.hashCode());return result;}@Overridepublic 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;}}
解决方案二:
暂时思路,先将相同的列去掉得到一个新的矩阵,然后求出这个矩阵的逆矩阵,将这两个矩阵补充为相同维数的矩阵(没有的用0补充),然后求他们的差,再根据矩阵的0来决策
解决方案三:
我觉得顺序无关紧要,可以排序,这样的算吗1,2,3 跟1,2。
解决方案四:
两行中只相差一个数字,1,2,3,4 和 5,3,2,1算不算?