美文网首页
LeetCode 01/06/18

LeetCode 01/06/18

作者: Muama | 来源:发表于2018-01-07 13:09 被阅读0次

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

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

相关文章

  • LeetCode 01/06/18

    早上咨询老师过后决定还是要去花时间搞几个大项目,没项目就只能当炮灰了,简历就要悲剧,没办法,同时搞吧。唉。。虽然有...

  • LeetCode 01/18/18

    array hopperII思路和I一样,也是从右向左. m[i] represents the min step...

  • 2021.9.13

    05:52-06:01 09:01-09:11 12:02-12:25 16:27-16:43 18:50-18:...

  • 2019-06-18

    2019-06-18日。 01:26 2019年6月18日 日精进。 体验。吸收 释放。 ...

  • 北极星双色球第17094期/

    龙头推荐:01 05 06 11 凤尾推荐:27 28 31 33 前区胆组:05 06 1112 18 28 1...

  • LeetCode 01/08/18 & 01/09/18

    哎哟 大周日的, 又是约饭又是要约吃鸡的,我只能说我尽量写几道题吧,真是堕落呀 Binary Tree Zigza...

  • LeetCode 01/13/18 & 01/15/18

    Permutations II N Queensdfs, N层每层N个可选位置,在是否加Q的地方限制条件是不在同一...

  • LeetCode 01/11/18 & 01/12/18

    陪suki去coffee chat,出去走了走,还上了willis tower,然而下雨,全都被云挡住了,好不容易...

  • LeetCode 01/05/18

    今天中午suki点了小米椒鸡丁,很好吃,起得一如既往地晚,起床玩了会儿就吃饭了,好吧,赶紧滚去刷题了。。 Bina...

  • LeetCode 01/04/18

    今天芝加哥虽然很冷,但阳光明媚,是个刷题的好日子。 昨天晚上跟孔神一番交流后感受到了巨大差距,无论是java基础,...

网友评论

      本文标题:LeetCode 01/06/18

      本文链接:https://www.haomeiwen.com/subject/yiounxtx.html