The Travels of Marco Cai

  • Home

  • About

  • Tags

  • Archives

  • Search

[LeetCode] Challenge log 292

Posted on 2018-04-29 | Edited on 2018-05-01 |
292. Nim Game

If n % 4 == 0, no matter how you act, your rival can make sure the next time you choose you still have n % 4 == 0.

Read more »

[LeetCode] Challenge log 136

Posted on 2018-04-29 | Edited on 2018-05-01 |
136. Single Number
  1. Use hash table to mark element that shows before.
  2. Use math, 2 * sum of all unique number - sum of all.
  3. Use a XOR 0 = a, a XOR a =0.
Read more »

[LeetCode] Challenge log 693

Posted on 2018-04-29 | Edited on 2018-05-01 |
693. Binary Number with Alterntive Bits

s1. n<<1 + n should be all 1s.

s2. similar to s1 but more pythonic

s3. check by bit

Read more »

[LeetCode] Challenge log 104

Posted on 2018-04-29 | Edited on 2018-05-01 |
104. Maximum Depth of Binary Tree

Root height = max(left height, right height) +1

Read more »

[LeetCode] Challenge log 226

Posted on 2018-04-29 | Edited on 2018-05-01 |
226. Invert Binary Tree

DFS will do.

Read more »

[LeetCode] Challenge log 521

Posted on 2018-04-29 |
521. Longest Ucommon Subsequence I

If they are not equal, the answer will be the longer string; otherwise, answer is not-existed(-1).

Read more »

[LeetCode] Challenge log 637

Posted on 2018-04-28 | Edited on 2018-04-29 |
637. Average of Levels in Binary Tree

s1. Depth first traverse, record every node’s (value, level)

s2. Width first traverse, keep two queues as parent level and kid level.

Read more »

[LeetCode] Challenge log 566

Posted on 2018-04-28 | Edited on 2018-04-29 |
566. Reshape the Matrix

Use python list slice and mod.

Read more »

[LeetCode] Challenge log 766

Posted on 2018-04-27 |
766. Toeplitz Matrix

Skip the first row and first column and traverse the matrix, check if m[i][j] = m[i-1][j-1].

Read more »

[LeetCode] Challenge log 463

Posted on 2018-04-27 |
463. Island Perimeter

s1: traverse all “1”, add the number of adjacent “0”s to the output. O(n) time.

s2: check every adjacent pairs horizontally and vertically, add 1 to the ouput if the pair is (“0”, “1”). In other words, we are checking every edge to check if it is a border. O(n) time but neater.

useful skill: map(list, zip(*grid)) = transpose(grid).

Read more »
1…678…13
Mingxiang Cai

Mingxiang Cai

A learner, a hobbyist.

128 posts
56 tags
© 2019 Mingxiang Cai
0%