(剑指offer)JAVA实现

题1: 二维数组中的查找
题目描述

在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
代码实现:

public class Solution {
    public boolean Find(int [][] array,int target) {
        if(array == null) {
            return false;
        }
        int row = 0, col = array[0].length - 1;
        while(row < array.length && col >= 0) {
            if(array[row][col] == target) {
                return true;
            }
            if(array[row][col] > target) {
                col--;
            }else {
                row++;
            }
        }
        return false;
    }
}

题2:替换空格
题目描述

请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
代码实现:

public class Solution {
    public String replaceSpace(StringBuffer str) {
        if(str == null) {
            return null;
        }
        StringBuffer out = new StringBuffer();
        for(int i = 0; i < str.length(); i++) {
            if(str.charAt(i) == ' ') {
                out.append("%");
                out.append("2");
                out.append("0");

            } else {
                out.append(String.valueOf(str.charAt(i)));
            }

        }
        return new String(out);
    }

}

题3:从尾到头打印链表

题目描述
输入一个链表,从尾到头打印链表每个节点的值。

/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {

         ArrayList<Integer> list = new ArrayList<Integer>();
         while(listNode != null) {
             list.add(0,listNode.val);
             listNode = listNode.next;
         }
        return list;

    }
}

题4:重建二叉树

题目描述

输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{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 TreeNode reConstructBinaryTree(int [] pre,int [] in) {
        boolean isHaveElement = false;
        if(pre == null || in == null) {
            return null;
        }
        if(pre.length != in.length) {
            return null;
        }
        TreeNode root = null;
        for(int i = 0; i < in.length; i++) {
            if(in[i] == pre [0]) {
                isHaveElement = true;
                root = new TreeNode(in[i]);
                root.left = reConstructBinaryTree(Arrays.copyOfRange(pre,1,i+1),Arrays.copyOfRange(in,0,i));
                root.right = reConstructBinaryTree(Arrays.copyOfRange(pre,i+1,pre.length),Arrays.copyOfRange(in,i+1,in.length));

            }
        }
        if(!isHaveElement) {
            return null;
        }
        return root;
    }
}

题5:用两个栈实现队列
题目描述
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

import java.util.Stack;

public class Solution {
    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.isEmpty() && stack2.isEmpty()) {
            throw new RuntimeException("非法输入");
        }else if (stack2.isEmpty()){
            while(!stack1.isEmpty()){
            stack2.push(stack1.pop());
            }
            return stack2.pop();
        }else{
            return stack2.pop();

        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值