Liam's Blog


  • Home

  • Archives

  • Tags

  • Categories

  • Photos

  • About

LeetCode #22 Generate Parentheses

Posted on 2018-09-05 | Post modified 2018-09-05 | In LeetCode , Difficulties , Algorithms , Medium , Backtracking

Problem

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

Example

given n = 3, a solution set is:

1
2
3
4
5
6
7
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
Read more »

LeetCode #78 Subsets

Posted on 2018-09-05 | Post modified 2018-09-05 | In Languages , LeetCode , Python , Difficulties , Algorithms , Medium , Backtracking

Problem

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example

1
2
3
4
5
6
7
8
9
10
11
12
Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
Read more »

LeetCode #111 Minimum Depth of Binary Tree

Posted on 2018-09-04 | Post modified 2018-10-10 | In Languages , LeetCode , Python , Difficulties , Algorithms , Data Structures , Easy , Binary Tree , Breadth-first Search

Problem

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

Example

Given binary tree [3,9,20,null,null,15,7],

1
2
3
4
5
  3
/ \
9 20
/ \
15 7

return its minimum depth = 2.

Note

A leaf is a node with no children.

Read more »

Computer Vision Notes (1)

Posted on 2018-09-04 | Post modified 2018-09-04 | In Languages , Python , Computer Vision , Notes , Tricks

Python generator and ‘yield’ statement

1
2
3
4
5
6
7
8
9
10
import numpy as np

def numbers(n):
for i in range(n):
num = np.random.randint(1, 40)
print('generated the {}-th number = {}'.format(i+1, num))
yield num

num_gen = numbers(10000000)
first = next(num_gen)
Read more »

LeetCode #100 Same Tree

Posted on 2018-09-04 | Post modified 2018-09-04 | In Languages , LeetCode , Python , Difficulties , Algorithms , Data Structures , Easy , Breadth First Search , Binary Tree

Problem

Given two binary trees, write a function to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical and the nodes have the same value.

Examples

1
2
3
4
5
6
7
Input:     1         1
/ \ / \
2 3 2 3

[1,2,3], [1,2,3]

Output: true
1
2
3
4
5
6
7
Input:     1         1
/ \
2 2

[1,2], [1,null,2]

Output: false
1
2
3
4
5
6
7
Input:     1         1
/ \ / \
2 1 1 2

[1,2,1], [1,1,2]

Output: false
Read more »

LeetCode #110 Balanced Binary Tree

Posted on 2018-09-04 | Post modified 2018-09-11 | In Languages , LeetCode , Python , Difficulties , Data Structures , Easy , Binary Tree

Problem

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as:

a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Examples

  • Given the following tree [3,9,20,null,null,15,7]:
1
2
3
4
5
  3
/ \
9 20
/ \
15 7

Return true.

  • Given the following tree [1,2,2,3,3,null,null,4,4]:

    1
    2
    3
    4
    5
    6
    7
          1
    / \
    2 2
    / \
    3 3
    / \
    4 4

    Return false.

Read more »

LeetCode #107 Binary Tree Level Order Traversal II

Posted on 2018-09-04 | Post modified 2018-10-10 | In Languages , LeetCode , Python , Difficulties , Algorithms , Data Structures , Easy , Binary Tree , Breadth-first Search , Depth-first Search

Problem

Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).

Example

Given binary tree [3,9,20,null,null,15,7],

1
2
3
4
5
  3
/ \
9 20
/ \
15 7

return its bottom-up level order traversal as:

1
2
3
4
5
[
[15,7],
[9,20],
[3]
]
Read more »

LeetCode #94 Binary Tree Inorder Traversal

Posted on 2018-09-04 | Post modified 2019-01-22 | In Languages , LeetCode , Python , Difficulties , Algorithms , Data Structures , Medium , Binary Tree , Depth-First Search

Problem

Given a binary tree, return the inorder traversal of its nodes’ values.

Example

1
2
3
4
5
6
7
8
Input: [1,null,2,3]
1
\
2
/
3

Output: [1,3,2]
Read more »

LeetCode #876 Middle of the Linked List

Posted on 2018-09-01 | Post modified 2018-09-04 | In Algorithms , Languages , LeetCode , Python , Two Pointers , Difficulties , Algorithms , Data Structures , Easy , Two Pointers , Linked List

Problem

Given a non-empty, singly linked list with head node head, return a middle node of linked list.

If there are two middle nodes, return the second middle node.

Examples

1
2
3
4
5
6
7
8
9
Input: [1,2,3,4,5]
Output: Node 3 from this list (Serialization: [3,4,5])
The returned node has value 3. (The judge's serialization of this node is [3,4,5]).
Note that we returned a ListNode object ans, such that:
ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL.

Input: [1,2,3,4,5,6]
Output: Node 4 from this list (Serialization: [4,5,6])
Since the list has two middle nodes with values 3 and 4, we return the second one.
Read more »

LeetCode #203 Remove Linked List Elements

Posted on 2018-09-01 | Post modified 2018-09-01 | In Algorithms , Languages , LeetCode , Python , Recursive , Difficulties , Algorithms , Data Structures , Easy , Recursive , Linked List

Problem

Remove all elements from a linked list of integers that have value val.

Example

1
2
Input:  1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5
Read more »
1…15161718
Liam Wang

Liam Wang

Liam's Personal Blog

174 posts
133 categories
64 tags
© 2019 Liam Wang
Powered by Hexo
Theme - NexT.Muse