剑指Offer(Java实现):顺时针打印矩阵、包含min函数的栈、栈的压入\弹出序列

package com.dengzm.jianzhioffer;

/**
 * @Description 029 顺时针打印矩阵
 * 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字
 *
 * Created by deng on 2019/5/2.
 */
public class Jianzhi029 {

    public static void main(String[] args) {
        int[][] nums = new int[3][3];
        nums[0][0] = 1;
        nums[0][1] = 2;
        nums[0][2] = 3;
        nums[1][0] = 4;
        nums[1][1] = 5;
        nums[1][2] = 6;
        nums[2][0] = 7;
        nums[2][1] = 8;
        nums[2][2] = 9;
        printMatrixClockwisely(nums);
    }

    private static void printMatrixClockwisely(int[][] nums) {
        if (nums == null || nums.length == 0) {
            return;
        }

        int columns = nums.length;
        int rows = nums[0].length;

        int start = 0;

        while (columns > start * 2 && rows > start * 2) {
            printMatrixInCircle(nums, columns, rows, start);
            ++ start;
        }
    }

    private static void printMatrixInCircle(int[][] nums, int columns, int rows, int start) {
        int endX = columns - 1 - start;
        int endY = rows - 1 - start;

        // 从左到右打印一行
        for (int i = start; i <= endY; i ++) {
            printNumber(nums[start][i]);
        }

        // 从上到下打印一列
        if (start < endY) {
            for (int i = start + 1; i <= endY; i ++) {
                printNumber(nums[i][endX]);
            }
        }

        // 从右到左打印一行
        if (start < endX && start < endY) {
            for (int i = endX - 1; i >= start; i --) {
                printNumber(nums[endY][i]);
            }
        }

        // 从下到上打印一列
        if (start < endX && start < endY - 1) {
            for (int i = endY - 1; i >= start + 1; i --) {
                printNumber(nums[i][start]);
            }
        }

    }

    private static void printNumber(int number) {
        System.out.print(number + " ");
    }
}

package com.dengzm.jianzhioffer;

import java.util.Stack;

/**
 * @Description 030 包含min函数的栈
 * 定义栈的数据结构,请在该类型中实现一个能得到栈的最小元素的min函数,且时间复杂度为O(1)
 *
 * Created by deng on 2019/5/21.
 */
public class Jianzhi030 {

    public static void main(String[] args) {
        StackWithMin<Integer> stackWithMin = new StackWithMin<>();
        stackWithMin.push(3);
        stackWithMin.push(4);
        stackWithMin.push(0);
        stackWithMin.push(2);
        stackWithMin.push(1);
        System.out.println(stackWithMin.min());
        stackWithMin.pop();
        System.out.println(stackWithMin.min());
        stackWithMin.pop();
        System.out.println(stackWithMin.min());
        stackWithMin.pop();
        System.out.println(stackWithMin.min());
        stackWithMin.pop();
        System.out.println(stackWithMin.min());
    }

    private static class StackWithMin<T extends Comparable> {
        private Stack<T> mData;
        private Stack<T> mMin;

        public StackWithMin() {
            mData = new Stack<>();
            mMin = new Stack<>();
        }

        public T pop() {
            mMin.pop();
            return mData.pop();
        }

        public T push(T data) {
            if (mMin.isEmpty()) {
                mMin.push(data);
            } else {
                T min = mMin.peek();
                if (data.compareTo(min) < 0) {
                    mMin.push(data);
                } else {
                    mMin.push(min);
                }
            }

            return mData.push(data);
        }

        public T min() {
            return mMin.peek();
        }
    }
}
package com.dengzm.jianzhioffer;

import java.util.Stack;

/**
 * @Description 031 栈的压入、弹出序列
 * 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是某栈的压栈序列。
 *
 * Created by deng on 2019/5/21.
 */
public class Jianzhi031 {

    public static void main(String[] args) {
        int[] push = new int[] {1,2,3,4,5};
        int[] pop1 = new int[] {4,5,3,2,1};
        int[] pop2 = new int[] {4,3,5,1,2};
        int[] pop3 = new int[] {4,3,5,2,1};

        System.out.println(isPopOrder(push, pop1));
        System.out.println(isPopOrder(push, pop2));
        System.out.println(isPopOrder(push, pop3));
    }

    private static boolean isPopOrder(int[] push, int[] pop) {
        if (push == null || pop == null || push.length == 0 || pop.length == 0 || push.length != pop.length) {
            return false;
        }

        boolean isPossable = false;

        Stack<Integer> stack = new Stack<>();

        int pushCur = 0;
        int popCur = 0;
        while (popCur < pop.length) {
            // 添加元素的情况 
            // 1.栈中元素已全部弹出,此时直接压栈 
            // 2.栈顶元素与当前弹出元素不相等
            while (stack.isEmpty() || stack.peek() != pop[popCur]) {
                // 若是当前序列错误,push已全部入栈,会快速结束循环,直到popCur为length
                if (pushCur == push.length) {
                    break;
                }

                stack.push(push[pushCur]);

                pushCur ++;
            }

            // 若是当前序列错误,push已全部入栈,会快速结束循环,直到popCur为length
            if (stack.peek() != pop[popCur]) {
                break;
            }

            stack.pop();
            popCur ++;
        }

        if (stack.isEmpty() && popCur == pop.length) {
            isPossable = true;
        }

        return isPossable;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值