LeetCode 01/06/18

早上咨询老师过后决定还是要去花时间搞几个大项目,没项目就只能当炮灰了,简历就要悲剧,没办法,同时搞吧。唉。。虽然有点失落但同时也消除了我的迷茫。知道了自己要做什么和怎么做之后,感觉整个人的状态都是不一样的,有一种使命感驱使我去完成每天的任务。
先刷几题冷静冷静

K Smallest In Unsorted Array
Find the K smallest numbers in an unsorted integer array A. The returned numbers should be in ascending order.
既可以用minHeap也可以用maxHeap来做,但最优解法是用quick select
注意maxHeap comparator的写法

PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(k, new Comparator<Integer>(){
      @Override
      public int compare(Integer o1, Integer o2) {
        if (o1.equals(o2)) {
          return 0;
        }
        return o1 > o2 ? -1 : 1;
      }
    });
  1. Kth Largest Element in an Array
    这题跟上一题一样解法

BFS

  1. Binary Tree Level Order Traversal
    典型bfs
while(!que.isEmpty()) {
            int size = que.size();
            List<Integer> lv = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                TreeNode cur = que.poll();
                if (cur.left != null) {
                    que.offer(cur.left);
                }
                if (cur.right != null) {
                    que.offer(cur.right);
                }
                lv.add(cur.val);
            }
            res.add(lv);
        }

***Bipartite
染色问题
maintain一个HashMap<GraphNode, Integer> Integer用来标记分组
通过BFS找neighbors, 然后分三种情况判断每个nei

1. unvisited -> visited.put() queue.offer()  
2. visited & visited.get(cur) != neiGroup -> return false
3. visited & visited.get(cur) = neiGroup -> do nothing, ignore

***check if binary tree is complete
case1: 出现了气泡,.right != null & .left == null
case2: one of the children is null
after detecting the first node that mises one child, then check whether all following nodes expanded to see whether they have any node generated(if any, return false)

  1. Kth Smallest Element in a Sorted Matrix
    留明天 今天来不及了 一会儿还要跟爸妈视频
  2. single num
    // N XOR 0 = N
    // N XOR N = 0
    // the XOR operator is commutative
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,975评论 0 33
  • 基本圆满处理完一天的事情,发现多和家长沟通,多去站在他们的角度去考虑问题!提高音频作业质量!坐上一辆人少的313,...
    害人精婷阅读 265评论 0 0
  • 光圈与快门速度的关系存在互补关系。 在同样的感光值下(ISO), 正确曝光时 大光圈配合更快的快门速度 小光圈配合...
    浅浅君子阅读 363评论 0 0
  • 最近,微博上的一则新闻引起了小编的注意,内容是这样的, 山西运城的张先生两月前被蚊子咬了一口,没想到脸肿得像篮球!...
    启恒数码阅读 263评论 0 2
  • 静静的坐在办公室里,白日的喧嚣早已遁去,只留下了宁静与淡泊。窗台上的花又长高了一大截,叶子圆圆的,像扇子,每一片都...
    珍琪一生阅读 220评论 0 0

友情链接更多精彩内容