算法练习1-10

/**

  • 1:题目描述:
  • 在一个二维数组中(每个一维数组的长度相同),
  • 每一行都按照从左到右递增的顺序排序,
  • 每一列都按照从上到下递增的顺序排序。
  • 请完成一个函数,输入这样的一个二维数组和一个整数,
  • 判断数组中是否含有该整数。
    */
public class Main1 {
/*    public boolean main1(int target, int [][] array) {
        for(int i=0; i<array.length; i++){
            for(int j=0; j<array[0].length; j++){
                if(target==array[i][j]){
                    return true;
                }else if(target<array[i][j]){
                    continue;
                }
            }
        }
        return false;
    }*/
    public boolean Main1(int target, int [][] array) {
        int row=array.length-1;
        int col=0;
        while(row>-1&&col<array[0].length){
            if(target==array[row][col]){
                return true;
            }else if(target>array[row][col]){
                col++;
            }else{
                row--;
            }
        }
        return false;
    }
}

/**

  • 2:题目描述
  • 请实现一个函数,将一个字符串中的每个空格替换成“%20”。
  • 例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
    */
public class Main2 {
    public String replaceSpace(StringBuffer str) {
        String st = new String();
        char[] c = str.toString().toCharArray();
        for(int i=0; i<c.length; i++){
            if(String.valueOf(c[i]).equals(" ")){
                st+="%20";
                continue;
            }
            st+=c[i];
        }
        return st;
    }
}

/**

  • 3:题目描述
  • 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
 *    public class Main3ListNode {
 *        int val;
 *        Main3ListNode next = null;
 *
 *        Main3ListNode(int val) {
 *            this.val = val;
 *        }
 *    }
 *
 */

public class Main3 {
    public ArrayList<Integer> printListFromTailToHead(Main3ListNode listNode) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        Stack <Integer> temp = new Stack<>();
        while(listNode!=null){
            temp.push(listNode.val);
            listNode=listNode.next;
        }
        while(!temp.empty()){
            arrayList.add(temp.pop());
        }
        return arrayList;
    }
}

/**

  • Definition for binary tree
  • public class TreeNode {
  • int val;
    
  • TreeNode left;
    
  • TreeNode right;
    
  • TreeNode(int x) { val = x; }
    
  • }
  • 4:题目描述
  • 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。
  • 假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
  • 例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
    */
public class Main4 {
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
        TreeNode root = reConstructBinaryTree(pre, 0, pre.length - 1, in, 0, in.length - 1);
        return root;
    }
    //前序遍历{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6}
    private TreeNode reConstructBinaryTree(int [] pre,int startPre,int endPre,int [] in,int startIn,int endIn) {
        if(startPre>endPre||startIn>endIn)
            return null;
        TreeNode root=new TreeNode(pre[startPre]);
        for(int i=startIn;i<=endIn;i++)
            if(in[i]==pre[startPre]){
                root.left=reConstructBinaryTree(pre,startPre+1,startPre+(i-startIn),in,startIn,i-1);
                root.right=reConstructBinaryTree(pre,startPre+(i-startIn)+1,endPre,in,i+1,endIn);
                break;
        }
        return root;
    }
}

/**

  • 5:题目描述
  • 用两个栈来实现一个队列,完成队列的Push和Pop操作。
  • 队列中的元素为int类型。
    */
public class Main5 {
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();
    //入队列
    public void push(int node) {
        stack1.push(node);
    }
    //出队列
    public int pop() {
        if(stack1.empty()&&stack2.empty()){
            throw new RuntimeException("Queue is empty!");
        }
        if(stack2.empty()){
            while(!stack1.empty()){
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }
}

/**

  • 6:题目描述
  • 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。
  • 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。
  • 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
  • NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
    */
public class Main6 {
    public int minNumberInRotateArray(int [] array) {
        if(array.length==0){
            return 0;
        }
        int i;
        for(i=0; i+1<array.length; i++){
            if(array[i]>array[i+1]){
                break;
            }
        }
        return array[i+1];
    }
}

/**

  • 7:题目描述
  • 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。
  • n<=39
    */
public class Main7 {
    public int Fibonacci(int n) {
        if(n<=0){
            return 0;
        }else if(n==1||n==2){
            return 1;
        }
        int a=1,b=1,c=0;
        for(int i=3; i<=n; i++){
            c=a+b;
            a=b;
            b=c;
        }
        return c;
    }
}

/**

  • 8:题目描述
  • 一只青蛙一次可以跳上1级台阶,也可以跳上2级。
  • 求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)
  • 找规律:归纳法
    */
public class Main8 {
    public int JumpFloor(int target) {
        if (target <= 0) {
            return -1;
        } else if (target == 1) {
            return 1;
        } else if (target ==2) {
            return 2;
        } else {
            return  JumpFloor(target-1)+JumpFloor(target-2);
        }
    }
}

/**

  • 9:题目描述
  • 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。
  • 求该青蛙跳上一个n级的台阶总共有多少种跳法。
    */
public class Main9 {
    public int JumpFloorII(int target) {
        if (target == 1) {
            return 1;
        } else {
            return 2 * JumpFloorII(target - 1);
        }
    }
}

/**

  • 10:题目描述
  • 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。
  • 请问用n个21的小矩形无重叠地覆盖一个2n的大矩形,总共有多少种方法?
    */
public class Main10 {
    public int RectCover(int target) {
        if(target<=0){
            return 0;
        }else if(target==1||target==2){
            return target;
        }else{
            return RectCover(target-1)+RectCover(target-2);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值