剑指offer题目--编程语言

数组

字符串

链表

单向链表

5 从头到尾打印链表

把链表的末尾结点的指针指向头结点,从而形成一个环形链表。

45 圆圈中最后剩下的数字

双向链表

27 二叉搜索树与双向链表

链表中的结点中除了有指向下一个结点的指针,还有指向任意结点的指针,即复杂链表。

26 复杂链表的复制

前序、中序、后续3种遍历都有循环和递归两种实现方法,递归实现较循环便捷很多。

39 二叉树的深度

18 树的子结构

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

6 重建二叉树

24 二叉树的后序遍历序列

宽度优先遍历:23 从上到下遍历二叉树

二叉搜索树

左子节点小于或等于根节点,右子节点大于或等于根节点。

50 树中两个结点的最低公共祖先

27 二叉搜索树与双向链表

堆和红黑树

堆分为最大堆和最小堆。最大堆中根节点值最大,最小堆中根节点值最小。很多需要快速找最大值或最小值的问题可以用堆来解决。

红黑树是把树中的结点定义为红、黑两种颜色,并通过规则确保从根结点到叶结点的最长路径长度不超过最短路径的两倍。

30 求最小的k个数字

栈和队列

7 用两个栈实现队列

3.搜索二维矩阵

class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix == null || matrix.length == 0){
return false;
}

boolean found = false;
if((matrix != null) && (matrix.length >0) && (matrix[0].length>0)){
int row = 0;
int column = matrix[0].length-1;
while((row < matrix.length)&&(column>=0)){
if(matrix[row][column]==target){
found = true;
break;
}else if(matrix[row][column]>target){
-- column;
}else{
++ row;
}
}
}
return found;
}
}

4.替换空格

双指针方式

public class Solution {
public String replaceSpace(StringBuffer str) {
int length = str.length();
if(str == null && length <= 0){
return str.toString();
}

int originalLength = 0;
int numberOfBlank = 0;
int i = 0;
while(i<length){
++ originalLength;
if(str.charAt(i) == ' '){
++ numberOfBlank;
}
i++;
}

int newLength = originalLength + numberOfBlank*2;
str.setLength(newLength);

int indexOfOriginal = originalLength-1;
int indexOfNew = newLength-1;
while(indexOfOriginal >=0 && indexOfNew >0){
if(str.charAt(indexOfOriginal) == ' '){
str.setCharAt(indexOfNew--,'0');
str.setCharAt(indexOfNew--,'2');
str.setCharAt(indexOfNew--,'%');
--indexOfOriginal;
}else{
str.setCharAt(indexOfNew--,str.charAt(indexOfOriginal));

--indexOfOriginal;
}
}

return str.toString();
}
}

stringbuffer方法

public class Solution {
public String replaceSpace(StringBuffer str) {
for(int i=0;i<str.length();i++){
if(str.charAt(i) == ' '){
str.replace(i,i+1,"%20");
}
}

return str.toString();
}
}

 

5.从尾到头打印链表

栈 先出后进

 1 import java.util.ArrayList;
 2 import java.util.Stack;
 3 public class Solution {
 4 public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
 5 Stack<Integer> stack=new Stack<Integer>();
 6 while(listNode!=null){
 7 stack.push(listNode.val);
 8 listNode=listNode.next; 
 9 }
10 
11 ArrayList<Integer> list=new ArrayList<Integer>();
12 while(!stack.isEmpty()){
13 list.add(stack.pop());
14 }
15 return list;
16 }
17 }

 

递归法

 1 import java.util.ArrayList;
 2 public class Solution {
 3 ArrayList<Integer> arrayList=new ArrayList<Integer>();
 4 public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
 5 if(listNode!=null){
 6 this.printListFromTailToHead(listNode.next);
 7 arrayList.add(listNode.val);
 8 }
 9 return arrayList;
10 }
11 }

6 重建二叉树

public class Solution {
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
        TreeNode root=reConstructBinaryTree(pre,0,pre.length-1,in,0,in.length-1);
        return root;
    }
    //前序遍历{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6}
    private TreeNode reConstructBinaryTree(int [] pre,int startPre,int endPre,int [] in,int startIn,int endIn) {

        if(startPre>endPre||startIn>endIn)
            return null;
        TreeNode root=new TreeNode(pre[startPre]);

        for(int i=startIn;i<=endIn;i++)
            if(in[i]==pre[startPre]){
                root.left=reConstructBinaryTree(pre,startPre+1,startPre+i-startIn,in,startIn,i-1);
                root.right=reConstructBinaryTree(pre,i-startIn+startPre+1,endPre,in,i+1,endIn);
                break;
            }

        return root;
    }
}

 

7 用两个栈实现队列

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);
    }
    
    public int pop() {
        if(stack2.size()<=0){
            while(stack1.size()>0){
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }
}

 

posted @ 2020-02-06 20:01  小仙女学编程  阅读(175)  评论(0)    收藏  举报