剑指offer题目--解决面试题的思路

19 二叉树的镜像

递归实现

public class Solution {
    public void Mirror(TreeNode root) {
        if(root == null || 
           (root.left == null && root.right == null)){
            return;
        }
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        if(root.left != null){
            Mirror(root.left);
        }
        if(root.right != null){
            Mirror(root.right);
        }
    }
}

循环实现

import java.util.Stack;
public class Solution {
    public void Mirror(TreeNode root) {
        if(root == null){
            return;
        }
        Stack<TreeNode> stack = new Stack<TreeNode>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode node = stack.pop();
            if(node.left != null||node.right != null){
                TreeNode temp = node.left;
                node.left = node.right;
                node.right = temp;
            }
            if(node.left!=null){
                stack.push(node.left);
            }
            if(node.right!=null){
                stack.push(node.right);
            }
        }
    }
}
View Code

20 顺时针打印矩阵

import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printMatrix(int [][] matrix) {
        ArrayList<Integer> result = new ArrayList<Integer> ();
        if(matrix.length==0) return result;
        int row = matrix.length,col = matrix[0].length;
        if(col==0) return result;
        
         // 定义四个关键变量,表示左上和右下的打印范围
        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)  result.add(matrix[top][i]);
            // top to bottom
            for (int i = top + 1; i <= bottom; ++i)  result.add(matrix[i][right]);
            // right to left
            if (top != bottom)
            for (int i = right - 1; i >= left; --i)  result.add(matrix[bottom][i]);
            // bottom to top
            if (left != right)
            for (int i = bottom - 1; i > top; --i)  result.add(matrix[i][left]);
            left++;top++;right--;bottom--;
        }
        return result;    
    }
}
View Code
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;
    }
};
View Code

21 包含min函数的栈

Stack实现

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);
        if(stack2.isEmpty() || node < stack2.peek()){
            stack2.push(node);
        }else{
            stack2.push(stack2.peek());
        }
    }
    
    public void pop() {
        assert(!stack1.isEmpty() && !stack2.isEmpty());
        stack1.pop();
        stack2.pop();
    }
    
    public int top() {
        return stack1.peek();
    }
    
    public int min() {
        return stack2.peek();
    }
}
View Code

ArrayList实现

import java.util.ArrayList;
 
public class Solution {
 
    ArrayList<Integer> list = new ArrayList<Integer>();
     
    public void push(int node) {
        list.add(0,node);
    }
     
    public void pop() {
        list.get(0);
        list.remove(0);
    }
     
    public int top() {
        return list.get(0).intValue();
    }
 
    public int min() {
        int temp = top();
        for(int i=1;i<list.size();i++){
            if(temp>list.get(i).intValue()){
                temp = list.get(i).intValue();
            }
        }
        return temp;
    }
}
View Code

22 栈的压入、弹出序列

 Stack实现

import java.util.ArrayList;
import java.util.Stack;

public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
      boolean bResult = false;
      int nLength = pushA.length;
      if(pushA != null && popA != null &&  nLength>0){
          int nextpush =0;
          int nextpop = 0;
          Stack<Integer> stackData = new Stack<Integer>();
          while(nextpop < nLength){
              while(stackData.empty() || stackData.peek() != popA[nextpop]){
                  if(nextpush == nLength){
                      break;
                  }
                  stackData.push(pushA[nextpush]);
                  nextpush++;
              }
              if(stackData.peek() != popA[nextpop]){
                  break;
              }
              stackData.pop();
              nextpop++;
          }
          if(stackData.empty() && nextpop == nLength){
              bResult = true;
          }
      }
        return bResult;
    }
}

23 从上往下打印二叉树

24 二叉搜索树的后序遍历序列

25 二叉树中和为某一值的路径

26 复杂链表的复制

27 二叉搜索树与双向链表

28 字符串的排列

猜你喜欢

转载自www.cnblogs.com/coding-fairyland/p/12301705.html