剑指offer(1/9)

顺时针打印矩阵

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printMatrix(int [][] matrix) {
       ArrayList<Integer> list=new ArrayList<>();
        int l=0,r=matrix[0].length-1;
        int t=0,b=matrix.length-1;
        while(true){
            for(int i=l;i<=r;i++){
                list.add(matrix[t][i]);
                
            }if(++t>b) break;
            for(int i=t;i<=b;i++){
                list.add(matrix[i][r]);
                
            }if(l>--r) break;
            for(int i=r;i>=l;i--){
                list.add(matrix[b][i]);
                
            }if(t>--b) break;
            for(int i=b;i>=t;i--){
                list.add(matrix[i][l]);
                
            }if(++l>r) break;
             
        }
        return list;
    }
}
//很惭愧,编译了多次才通过的代码。。
/*
    思想,用左上和右下的坐标定位出一次要旋转打印的数据,一次旋转打印结束后,往对角分别前进和后退一个单位。
    提交代码时,主要的问题出在没有控制好后两个for循环,需要加入条件判断,防止出现单行或者单列的情况。
 */
class Solution {
public:
    vector<int> printMatrix(vector<vector<int> > matrix) {
		int row = matrix.size();
        int col = matrix[0].size();
        vector<int> res;
        
        // 输入的二维数组非法,返回空的数组
        if (row == 0 || col == 0)  return res;
        
        // 定义四个关键变量,表示左上和右下的打印范围
        int left = 0, top = 0, right = col - 1, bottom = row - 1;
        while (left <= right && top <= bottom)
        {
            // left to right
            for (int i = left; i <= right; ++i)  res.push_back(matrix[top][i]);
            // top to bottom
            for (int i = top + 1; i <= bottom; ++i)  res.push_back(matrix[i][right]);
            // right to left
            if (top != bottom)
            for (int i = right - 1; i >= left; --i)  res.push_back(matrix[bottom][i]);
            // bottom to top
            if (left != right)
            for (int i = bottom - 1; i > top; --i)  res.push_back(matrix[i][left]);
            left++,top++,right--,bottom--;
        }
        return res;
    }
};

包含min函数的栈

import java.util.Stack;

public class Solution {

    private Stack<Integer> data = new Stack<>();
    private Stack<Integer> min = new Stack<>();
    
    //public Solution(){
        //this.data = new Stack<>();
        //this.min = new Stack<>();
    //}
    
    public void push(int node) {
        data.push(node);
        if(min.empty()){
            min.push(node);
        }else{
            min.push(node <= min.peek()? node : min.peek());
        }
    }
    
    public void pop() {
        data.pop();
        min.pop();
    }
    
    public int top() {
        return data.peek();
    }
    
    public int min() { 
        return min.peek();
    }
}
class Solution {
public:
    void push(int value) {
        st.push(value);
        if(smin.empty())
            smin.push(value);
        if(smin.top()>value)
            smin.push(value);
    }
    void pop() {
        if(smin.top()==st.top())
            smin.pop();
        st.pop();
    }
    int top() {
        return st.top();
    }
    int min() {
        return smin.top();
    }
    private:
    stack<int> st;
    stack<int> smin;
};

栈的压入、弹出

输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)

import java.util.Stack;

public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
        Stack<Integer> stack = new Stack<>();
        int idx = 0;
        for(int i = 0; i < pushA.length; i++){
            stack.push(pushA[i]);
            while(!stack.isEmpty() && popA[idx] == stack.peek()){
                stack.pop();
                idx ++;
            }
        }
        return stack.isEmpty();
    }
}
class Solution {
public:
    bool IsPopOrder(vector<int> pushV,vector<int> popV) {
        if (pushV.size()==0) return false;
        stack<int> s;
        for (int i=0, j=0; i < pushV.size(); i++){
            s.push(pushV[i]);
            while(j<popV.size() && s.top()==popV[j]){
                s.pop();
                j++;
            }
        }
        return s.empty();
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值