Liam's Blog


  • Home

  • Archives

  • Tags

  • Categories

  • Photos

  • About

LeetCode #103 Binary Tree Zigzag Level Order Traversal

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

Problem

Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between).

Example

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

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

return its zigzag level order traversal as:

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

LeetCode #147 Insertion Sort List

Posted on 2018-09-18 | Post modified 2018-09-19 | In Languages , LeetCode , Python , Difficulties , Data Structures , Medium , Linked List

Problem

Sort a linked list using insertion sort.

img
A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.
With each iteration one element (red) is removed from the input data and inserted in-place into the sorted list

Algorithm of Insertion Sort:

  1. Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list.
  2. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there.
  3. It repeats until no input elements remain.

Examples

1
2
Input: 4->2->1->3
Output: 1->2->3->4
1
2
Input: -1->5->3->4->0
Output: -1->0->3->4->5
Read more »

LeetCode #39 Combination Sum

Posted on 2018-09-18 | Post modified 2018-09-18 | In Languages , LeetCode , Python , Difficulties , Algorithms , Data Structures , Medium , Backtracking , Array

Problem

Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.

The same repeated number may be chosen from candidates unlimited number of times.

Examples

1
2
3
4
5
6
Input: candidates = [2,3,6,7], target = 7,
A solution set is:
[
[7],
[2,2,3]
]
1
2
3
4
5
6
7
Input: candidates = [2,3,5], target = 8,
A solution set is:
[
[2,2,2,2],
[2,3,3],
[3,5]
]

Boundary Conditions

Read more »

LeetCode #24 Swap Nodes in Pairs

Posted on 2018-09-18 | Post modified 2018-09-18

Problem

Given a linked list, swap every two adjacent nodes and return its head.

Example

1
Given 1->2->3->4, you should return the list as 2->1->4->3.

Note

Can solve this problem by using only constant extra space.

Read more »

LeetCode #3 Longest Substring Without Repeating Characters

Posted on 2018-09-18 | Post modified 2018-09-18 | In Languages , LeetCode , Python , Difficulties , Algorithms , Data Structures , Medium , Two Pointers , Dynamic Programming , String , Dictionary

Problem

Given a string, find the length of the longest substring without repeating characters.

Examples

1
2
3
Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
1
2
3
Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
1
2
3
4
Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
Read more »

Data Science Data Cleaning

Posted on 2018-09-18 | Post modified 2018-09-18 | In Data Science

Cleaning Data: Garbage In, Garbage Out

Many issues arise in ensuring the sensible analysis of data from the field, including:

  • Distinguishing errors from artifacts
  • Data compatibility/unification
  • Imputation of missing values
  • Estimating unobserved (zero) counts
  • Outlier detection
Read more »

Structures, Lists, Difference Lists (Part 2)

Posted on 2018-09-17 | Post modified 2018-10-15 | In Languages , Prolog , Concepts , Queries , Declarative Meaning , Procedural Meaning

Prolog Execution

Call: Call a predicate(invocation)
Exit: Return an answer to the caller
Fail: Return to caller with no answer
Redo: Try next path to find an answer
Read more »

LeetCode #15 3Sum

Posted on 2018-09-17 | Post modified 2018-09-18 | In Languages , LeetCode , Python , Difficulties , Data Structures , Medium , Array

Problem

Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Example

1
2
3
4
5
6
7
Given array nums = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
Read more »

LeetCode #257 Binary Tree Paths

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

Problem

Given a binary tree, return all root-to-leaf paths.

Example

1
2
3
4
5
6
7
8
9
10
11
Input:

1
/ \
2 3
\
5

Output: ["1->2->5", "1->3"]

Explanation: All root-to-leaf paths are: 1->2->5, 1->3
Read more »

LeetCode #167 Two Sum II - Input array is sorted

Posted on 2018-09-17 | Post modified 2018-09-17 | In Languages , LeetCode , Python , Difficulties , Algorithms , Data Structures , Easy , Two Pointers , Array

Problem

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.

Example

1
2
3
Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.

Note

  • Your returned answers (both index1 and index2) are not zero-based.
  • You may assume that each input would have exactly one solution and you may not use the sameelement twice.
Read more »
1…101112…18
Liam Wang

Liam Wang

Liam's Personal Blog

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