剑指offerJAVA版笔记(第一天、第一题至第六题)

1.二维数组中的查找

  • 题目描述:在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
  • 解题思路:因为是有序的二维数组,我们可以从数组的左下角开始查询,当数小时候就往右边查,当数大了就往左边查询。
public class Solution {
    public boolean Find(int target, int [][] array) {
        int len = array.length-1;
        int i = 0;
        while((len >= 0)&& (i < array[0].length)){
            if(array[len][i] > target){
                len--;
            }else if(array[len][i] < target){
                i++;
            }else{
                return true;
            }
        }
        return false;
    }
}

2.替换空格

  • 题目描述:请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
  • 解题思路:逐个读取字符串的字符当为空格时使用replace函数经行替换即可。
public class Solution {
    public String replaceSpace(StringBuffer str) {
      int len = str.length() - 1;
        while (len >= 0) {
            char sign = str.charAt(len);
            if (sign == ' ') {
                str.replace(len, len+1, "%20");
            }
            len--;
        }
        return str.toString();
}
}

3.从尾到头打印链表

  • 题目描述:输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
  • 解题思路:使用HashMap存储整个链表的顺序结构结构,再逆序存入ArrayList,返回结果。
public class Solution {
   public static ArrayList<Integer> printListFromTailToHead(ListNode listNode){
        Map<Integer,ListNode> map = new HashMap<>();
        int num = 0;
        ArrayList<Integer> result = new ArrayList();
        while(listNode != null){
            map.put(num,listNode);
            num ++;
            listNode = listNode.next;
        }
        num--;
        for (;num >= 0 ;num--){
            result.add(map.get(num).val);
        }
        return result;
    }
}

4.重建二叉树

  • 题目描述:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
  • 解题思路:前序的第一个元素为i,找到中序中i的位置,左边的即为左子树上的节点,右边即为右子树上的节点,递归即可重建二叉树。
public class Solution {
    public TreeNode reConstructBinaryTree(int [] pre,int [] in)throws Exception {
         if(pre == null || in == null){
            return null;
        }
        if(pre.length != in.length){
            throw new Exception("长度不一样,非法的输入");
        }
        TreeNode root = null;
        for (int i = 0; i < in.length ; i++) {
            if (in[i] == pre[0]){
                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));

            }
        }
        return root;
    }
}

5.用两个栈实现队列

  • 题目描述:用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
  • 解题思路:push操作时对stack1做push。pop时先判断stack2是否为空,若为空,将stack1中的所有元素push进stack2,然后pop stack2中的元素。
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(stack2.isEmpty()){
            while (!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }
}

6.旋转数组的最小数字

  • 题目描述:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
  • 解题思路:因为是一个递增排序,将排序的头部移位至尾部,只需要找到array[ i ] > array[ i + 1],此时即为最小的那个数字
public class Solution {
    public int minNumberInRotateArray(int [] array) {
    if (array.length == 0){
            return 0;
        }
        if (array.length == 1){
            return  array[0];
        }
        for (int i = 0; i < array.length - 1 ; i++) {
            if (array[i] > array[i + 1]){
                return array[i + 1];
            }else{
                if (i == array.length - 2){
                    return array[0];
                }
            }
        }
        return 0;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值