sshzhangwg 2012-04-28 14:09
浏览 257
已采纳

高分求解基本的java算法题

例如:
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万。

  • 写回答

4条回答

  • iteye_3843 2012-04-28 17:13
    关注

    试写一个:
    [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]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题