算法实践(一)

首先看第一个问题:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ,则依次打印出数字

1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

public class Solution {
    public ArrayList printMatrix(int[][] array) 
    {
       int row1=0,col1=0,row2=array.length-1,col2=array[0].length-1;   
       ArrayList<Integer> res = new ArrayList();
       while (row1<=row2 && col1<=col2){
         ArrayList<Integer> temp = printMatrix1(array,row1++,col1++,row2--,col2--);
         res.addAll(temp);
        }
       return res;
    }

public ArrayList<Integer> printMatrix1(int[][] array,int row1,int col1,int row2, int col2){
    ArrayList<Integer> temp = new ArrayList();
    if (array==null) return temp;

    int curR=row1,curC=col1;
    if (row1==row2){
        for (curC = col1; curC <= col2; curC++) {
            temp.add(array[row1][curC]);
        }
    }else if (col1==col2){
        for (curR = row1; curR <= row2; curR++) {
            temp.add(array[curR][col1]);
        }
    }else {
        while (curC<col2){
            temp.add(array[row1][curC++]);
        }
        while (curR<row2){
            temp.add(array[curR++][col2]);
        }
        while (curC>col1){
            temp.add(array[row2][curC--]);
        }
        while (curR>row1){
            temp.add(array[curR--][col1]);
        }
    }
    return temp;
}}

接着看第二个问题:

输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。

下面是优化后的代码。第一个解决这个问题的时候对于递归的传入参数是两个:左子树的子数组和右子树的子数组,但是这样做的话就需要两个循环来生成子数组。得益于上一道问题的启发。传入参数改成了数组的下标范围。

最后总结:与其要求出当前数组或者矩阵的子数组、子矩阵,不如函数传参的时候直接传入下标(也就是范围),这样通过控制传入的范围就能实现操作子集的作用了,而不是再去另外求子集了(原始结构的参数坑会限制住思维,所以每一个都是重新构建一个函数,在给出的主函数里调用),也算是自己实践的一点小收获,记录下来。

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
import java.util.Arrays;
public class Solution {
    public static TreeNode reConstructBinaryTree(int[] preS,int[] inS) {
        return ConstructBinaryTree(preS,0,preS.length-1,inS,0,inS.length-1);
    }
    public static TreeNode ConstructBinaryTree(int[] preS,int left,int right,int[] inS,int sleft,int sright)
    {
        if (preS == null || inS == null || 
        preS.length==0 || inS.length==0 || left>right || sleft>sright) return null;
        int p = preS[left];
        int index = 0;
        for(int i=sleft;i<=sright;i++){
            if(inS[i]==p){
                index = i;
                break;
            }
        }
        TreeNode root = new TreeNode(p);
        root.left = ConstructBinaryTree(preS,left+1,index+left-sleft, inS,sleft,index-1);
        root.right = ConstructBinaryTree(preS,left+index+1-sleft,right, inS,index+1,sright);
        return root;
    }
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值