leetcode学习02

LeetCode树

144二叉树的前序遍历

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
         List<Integer> res = new ArrayList<Integer>();
         preorder(root,res);
         return res;

    }
    public void preorder(TreeNode root,List<Integer> res){
        if(root==null){
            return;
        }
        res.add(root.val);
        preorder(root.left,res);
        preorder(root.right,res);
    }
}

94 二叉树的中序遍历

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
      List<Integer> res = new ArrayList<Integer>();
      inorder(root,res);
      return res;
    }
    public void inorder(TreeNode root,List<Integer> res){
        if(root==null){
            return;
        }
        inorder(root.left,res);
        res.add(root.val);
        inorder(root.right,res);
    }
}

145二叉树的后序遍历

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
      List<Integer> res = new ArrayList<Integer>();
      postorder(root,res);
      return res;
    }
    public void postorder(TreeNode root,List<Integer> res){
        if(root==null){
            return;
        }
        postorder(root.left,res);
        postorder(root.right,res);
        res.add(root.val);
    }
}

常用堆操作

//堆化操作是O(N)的时间复杂度
//创建小根堆
PriorityQueue<Integer> minheap = new PriorityQueue<>();
//创建大根堆
PriorityQueue<Integer> maxheap = new PriorityQueue<>(Collections.reverseOrder());
//添加元素
minheap.add(10);
maxheap.add(2);
//查找堆顶元素
minheap.peek();
maxheap.peek();
//删除堆顶元素
minheap.poll();
maxheap.poll();
//大小
minheap.size();
maxheap.size();
//遍历,边删除边遍历
while(!minheap.isEmpty()){
    System.out.println(minheap.poll());
}

LeetCode堆

215数组中第k个最大元素

在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。

示例 1:

输入: [3,2,1,5,6,4] 和 k = 2 输出: 5

class Solution {
    public int findKthLargest(int[] nums, int k) {
       if(nums==null || nums.length<k ||k==0){
           return Integer.MIN_VALUE;
       }
       PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
       for(int num:nums){
           pq.add(num);
       }
       while(k>1){
           pq.poll();
           k--;
       }
       return pq.poll();
    }
}

692前K个高频单词

给一非空的单词列表,返回前 k 个出现次数最多的单词。

返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率,按字母顺序排序。

示例 1:

输入: ["i", "love", "leetcode", "i", "love", "coding"], k = 2 输出: ["i", "love"]

解析: "i" 和 "love" 为出现次数最多的两个单词,均为2次。 注意,按字母顺序 "i" 在 "love" 之前。

class Solution {//小根堆+哈希表
    public List<String> topKFrequent(String[] words, int k) {
       HashMap<String,Integer> mapping = new HashMap<>();
       for(String word :words){
           if(!mapping.containsKey(word)){
               mapping.put(word,0);
           }
           int count = mapping.get(word)+1;
           mapping.put(word,count);
       }
       PriorityQueue<String> pq = new PriorityQueue<>(
           (w1,w2)-> mapping.get(w1).equals(mapping.get(w2)) ? w2.compareTo(w1) : mapping.get(w1)- mapping.get(w2)
       );
       for(String word: mapping.keySet()){
           pq.add(word);
           if(pq.size()>k){
               pq.poll();
           }
       }
       List<String> res = new ArrayList<>();
        while(!pq.isEmpty()){
            res.add(pq.poll());
        }
        Collections.reverse(res);
        return res;
       }
        
}

 

原文地址:https://www.cnblogs.com/asako/p/14787377.html