Java《剑指offer》编程练习

题目:牛客网《剑指offer》

1. 数组

1-1. 二维数组中的查找

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

public class FindInDoubleDimensionalArray {

    public static void main(String[] args) {
//        int [][] array = {{1,3,5,7},{11,13,15,17},{21,23,25,27},{31,33,35,37}};
//        int [][] array = {{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
        int [][] array = {{}};
        System.out.println(Find(5, array));
    }

    public static boolean Find(int target, int [][] array) {
        if (array.length == 0) {
            return false;
        }
        for (int i = 0; i < array.length; i++) {
            if (array[i].length == 0) {
                return false;
            }
            if (array[i][0] == target) {
                return true;
            } else if (array[i][0] < target) {
                for (int j = 1; j < array[i].length; j++) {
                    if (array[i][j] == target) {
                        return true;
                    }
                }
            } else {
                break;
            }
        }
        return false;
    }
}

2. 字符串

2-1. 替换空格

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

public class ReplaceSpace {

    public static void main(String[] args) {
        StringBuffer str1 = new StringBuffer("We Are Happy");
        StringBuffer str2 = new StringBuffer("Happy");
        StringBuffer str3 = new StringBuffer("");
        System.out.println(replaceSpace(str1));
        System.out.println(replaceSpace(str2));
        System.out.println(replaceSpace(str3));
    }

    public static String replaceSpace(StringBuffer str) {
        if (str.capacity() == 0) {
            return str.toString();
        }
        for (int i = 0; i < str.capacity(); i++) {
            int start = str.indexOf(" ");
            if (start == -1) {
                break;
            }
            str.delete(start, start + 1).insert(start, "%20");
        }
        return str.toString();
    }
    
    //第二种方法:利用API替换掉所用空格,一行代码解决问题
 	public static String replaceSpace2(StringBuffer str) {
		return str.toString().replaceAll("\\s", "%20");
  	}
}

3. 链表

3-1. 从尾到头打印链表

输入一个链表,按链表从尾到头的顺序返回一个ArrayList。

public class ReversePrintLinkedList {

    public static void main(String[] args) {
        ListNode n1 = new ListNode(11);
        ListNode n2 = new ListNode(12);
        ListNode n3 = new ListNode(13);
        ListNode n4 = new ListNode(14);
        ListNode n5 = new ListNode(15);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        System.out.println(printListFromTailToHead(n1));
    }

    public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        if (listNode == null) {
            return arrayList;
        }
        arrayList.add(listNode.val);
        while (listNode.next != null) {
            listNode = listNode.next;
            arrayList.add(listNode.val);
        }
        for (int i = 0; i < arrayList.size() >> 1; i++) {
            int temp = arrayList.get(i);
            arrayList.set(i, arrayList.get(arrayList.size() - 1 - i));
            arrayList.set(arrayList.size() - 1 - i, temp);
        }
        return arrayList;
    }

    static class ListNode {
        int val;
        ListNode next = null;
        ListNode(int val) {
            this.val = val;
        }
    }
}

4. 树

4-1. 重建二叉树

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

public class RebuildBinaryTree {

    //        1
    //    2        3
    // 4        5     6
    //  7            8
    public static void main(String[] args) {
        int[] pre = {1,2,4,7,3,5,6,8};
        int[] in = {4,7,2,1,5,3,8,6};
        TreeNode treeNode = reConstructBinaryTree(pre, in);
        printNode(treeNode);
    }

    public static TreeNode reConstructBinaryTree(int [] pre, int [] in) {
        if (pre.length == 0 || in.length == 0) {
            return null;
        }
        TreeNode root = new TreeNode(pre[0]);
        if (pre.length == 1 && in.length == 1) {
            return root;
        }
        int leftNum = 0;
        for (int i = 0; i < in.length; i++) {
            if (in[i] == root.val) {
                break;
            }
            leftNum++;
        }
        int rightNum = in.length - 1 - leftNum;
        int[] inLeft = new int[leftNum];
        for (int i = 0; i < leftNum; i++) {
            inLeft[i] = in[i];
        }
        int[] inRight = new int[rightNum];
        for (int i = 0, j = leftNum + 1; j < in.length; i++,j++) {
            inRight[i] = in[j];
        }
        int[] preLeft = new int[leftNum];
        for (int i = 0,j = 1; j <= leftNum; i++,j++) {
            preLeft[i] = pre[j];
        }
        int[] preRight = new int[rightNum];
        for (int i = 0,j = leftNum + 1; j < pre.length; i++,j++) {
            preRight[i] = pre[j];
        }
        root.left = reConstructBinaryTree(preLeft, inLeft);
        root.right = reConstructBinaryTree(preRight, inRight);
        return root;
    }

    static public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }

    public static void printNode(TreeNode treeNode) {
        System.out.print(treeNode.val);
        System.out.print(' ');
        if (treeNode.left != null) {
            printNode(treeNode.left);
        }
        if (treeNode.right != null) {
            printNode(treeNode.right);
        }
    }
}

5. 栈和队列

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值