剑指offer21-30

栈的压入,弹出序列

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

public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
        //private Stack<Integer>stack=new Stack<Integer>();
        //Stack<Integer>stack=new Stack();
        Stack <Integer>stack=new Stack<Integer>();
        int m=pushA.length;
        int j=0;
        for(int i=0;i<m;i++)
        {
            stack.push(pushA[i]);
            while(j<m&&stack.peek()==popA[j])
            {
                stack.pop();
                j++;
            }
        }
        return stack.isEmpty();
      
    }
}
从上向下打印二叉树
import java.util.ArrayList;
import java.util.Queue;
import java.util.LinkedList;
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
        Queue<TreeNode> queue=new LinkedList<TreeNode>();
        ArrayList<Integer> list=new ArrayList<Integer>();
        if(root==null)
            return list;
        queue.add(root);
        while(!queue.isEmpty())
        {
            int size=queue.size();
            while(size-->0)
            {
                TreeNode tree=queue.poll();//去除队列中的第一个元素并进行记录
                if(tree.left!=null)
                    queue.add(tree.left);
                if(tree.right!=null)
                    queue.add(tree.right);
                list.add(tree.val);
            }
        }
        return list;
        
    }
}

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

public class Solution {
    public boolean VerifySquenceOfBST(int [] sequence) {
        if(sequence==null||sequence.length==0)
            return false;
        return Verify(sequence,0,sequence.length-1);//Verify---验证
    }
    private  boolean  Verify(int [] sequence,int first,int last)
    {
        if(last-first<=1)
            return true;
        int rootval=sequence[last];
        int cutindex=first;
        while(cutindex<last&&sequence[cutindex]<=rootval)
        {
            cutindex++;
        }
        for(int i=cutindex+1;i<last;i++)
        {
            if(sequence[i]<rootval)
                return false;
        }
        return Verify(sequence,first,cutindex-1)&&Verify(sequence,cutindex,last-1);
    }
}
//由于是对二叉搜索树进行后序遍历所以根节点一定是在最后面,左子树的节点一定小于根节点,右子树的节点一定大于根节点
//所以先找到第一个大于根节点的节点,如果他是右子树的第一个节点,那他的后面一定是大于根节点的然后逐层进行递归
//直到找到最后一个二叉树即last-first<=1

二叉树中和为某一值得路径

import java.util.ArrayList;
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    ArrayList<ArrayList<Integer>> ret=new  ArrayList<ArrayList<Integer>>();//设为全局变量
    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
        backtracking(root,target,new ArrayList<Integer>());
        return ret;
    }
    private void backtracking(TreeNode node,int target,ArrayList<Integer> path)
    {
        if(node==null)
            return;//最后没有找到返回
        path.add(node.val);
        target-=node.val;
        if(target==0&&node.left==null&&node.right==null)
            ret.add(new ArrayList(path));
        else
        {
            backtracking(node.left,target,path);
            backtracking(node.right,target,path);
        }
        path.remove(path.size()-1);//当递归到最后一个节点没有找到时,回退一步继续找
    }
}

复杂链表的复制

/*
public class RandomListNode {
    int label;
    RandomListNode next = null;
    RandomListNode random = null;

    RandomListNode(int label) {
        this.label = label;
    }
}
*/
public class Solution {
    public RandomListNode Clone(RandomListNode pHead)
    {
        if(pHead==null)
            return null;
        //插入新节点
        RandomListNode cur=pHead;
        while(cur!=null)
        {
            RandomListNode clone=new RandomListNode(cur.label);
            clone.next=cur.next;
            cur.next=clone;
            cur=clone.next;
        }
        //挂Random的链
        cur=pHead;
        while(cur!=null)
        {
            RandomListNode clone=cur.next;
            if(cur.random!=null)
            {
                 clone.random=cur.random.next;//必须进行判断防止cur.random为null;
            }
            cur=clone.next;
        }
        //长链表进行分离
        cur=pHead;
        RandomListNode cloneHead=pHead.next;
        while(cur.next!=null)
        {
            RandomListNode next=cur.next;
            cur.next=next.next;
            cur=next;
        }
        return cloneHead;
      //如果要对最后一个链表进行操作条件为cur.next!=null否则条件为cur!=null  特别注意
    }
}

二叉搜索树与双向链表

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    TreeNode pre=null;//记录传入节点的前一个节点
    TreeNode head=null;
    public TreeNode Convert(TreeNode pRootOfTree) {
        if(pRootOfTree==null)
            return null;
        DoubleSortNode(pRootOfTree);
        return head;
    }
    private void  DoubleSortNode(TreeNode node)
    {
        if(node==null)
            return;
        DoubleSortNode(node.left);
        node.left=pre;//记录此时传入节点的左节点的指向
        if(pre!=null)
        {
            pre.right=node;//指向传入节点
        }
        pre=node;//移动节点
        if(head==null)
            head=node;//确定根节点的位置
        DoubleSortNode(node.right);
    }
}

字符串的排列

import java.util.ArrayList;
import java.util.Arrays;
public class Solution {
	public ArrayList<String> ret=new ArrayList<String>();
    public ArrayList<String> Permutation(String str) {
        if(str.length()==0)
            return ret;
        char []chars=str.toCharArray();
        Arrays.sort(chars);//先对传入的数组进行排序
        backtracking(chars,new boolean[chars.length],new StringBuffer());
        return ret;
           
    }
    private void backtracking(char []chars,boolean []hasused,StringBuffer s) {
    	if(s.length()==chars.length)
    	{
    		ret.add(s.toString());
    		return ;//如果出现了一种达到要求的情况记录下来并回溯
    	}
    	for(int i=0;i<chars.length;i++)
    	{
    		if(hasused[i])
    			continue;//不能传入重复的
    		if(i!=0&&chars[i]==chars[i-1]&&!hasused[i-1])//防止有两个相同的情况)//防止重复
    			continue;
    		hasused[i]=true;
    		s.append(chars[i]);
    		backtracking(chars,hasused,s);
    		s.deleteCharAt(s.length()-1);
    		hasused[i]=false;

    	}
    }
}
数组中出现次数超过一半的数字
public class Solution {
   public int MoreThanHalfNum_Solution(int[] array)
   {
       int number = array[0];
       for (int i = 1, cnt = 1; i < array.length; i++) 
       {
           cnt = array[i] == number ? cnt + 1 : cnt - 1;
           if (cnt == 0)
           {
               number = array[i];
               cnt = 1;
           }
       }
       int cnt = 0;
       for (int val : array)
       {
           if (val == number)
           {
               cnt++;
           }
       }
       return cnt > array.length / 2 ? number : 0;
   }
}
//当一个数组中如果有超过一半的数字时那就意味着必须至少有两个这样的数字在一块连着而且cnt不会等于0
//循环出来如果有这个数字那么一定是number,再进行遍历数组操作

连续子数组的最大和

public class Solution {
    public int FindGreatestSumOfSubArray(int[] array) {
        int sum=0;
        int ret=Integer.MIN_VALUE;
        for(int val:array)
        {
            sum=sum<=0?val:sum+val;
            ret=Math.max(sum,ret);
        }
        return ret;
        
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值