Liam's Blog


  • Home

  • Archives

  • Tags

  • Categories

  • Photos

  • About

Data Science with Python I

Posted on 2018-09-08 | Post modified 2018-09-12 | In Languages , Data Science , Python , Basic

Basic Process and Possible Python Packages

Step 1. Get Data

  • Beautiful Soup - deals with HTML and XML

    Read more »

LeetCode #229 Majority Element II

Posted on 2018-09-07 | Post modified 2018-09-12 | In Languages , LeetCode , Python , Difficulties , Data Structures , Medium , Array , Built-in , Counter

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.

Note: The algorithm should run in linear time and in O(1) space.

Example 1:

1
2
Input: [3,2,3]
Output: [3]

Example 2:

1
2
Input: [1,1,1,3,3,2,2,2]
Output: [1,2]

Solution

Method:

Time Complexity:

Space Complexity:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
def majorityElement(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
from collections import Counter
dictionary = Counter(nums)
ret = []
majority = len(nums)//3

for idx, key in enumerate(dictionary):
if dictionary[key] > majority:
ret.append(key)

return ret

LeetCode #88 Merge Sorted Array

Posted on 2018-09-07 | Post modified 2018-09-08 | In Languages , LeetCode , Python , Difficulties , Data Structures , Easy , Array

Problem

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

Note

  • The number of elements initialized in nums1 and nums2 are m and n respectively.
  • You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.

Example

1
2
3
4
5
Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3

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

LeetCode #237 Delete Node in a Linked List

Posted on 2018-09-07 | Post modified 2018-09-08 | In Languages , LeetCode , Python , Difficulties , Data Structures , Easy , Linked List

Problem

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

Examples

1
2
3
4
Input: head = [4,5,1,9], node = 5
Output: [4,1,9]
Explanation: You are given the second node with value 5, the linked list
should become 4 -> 1 -> 9 after calling your function.
1
2
3
4
Input: head = [4,5,1,9], node = 1
Output: [4,5,9]
Explanation: You are given the third node with value 1, the linked list
should become 4 -> 5 -> 9 after calling your function.

Note

  • The linked list will have at least two elements.
  • All of the nodes’ values will be unique.
  • The given node will not be the tail and it will always be a valid node of the linked list.
  • Do not return anything from your function.
Read more »

LeetCode #21 Merge Two Sorted Lists

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

Problem

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Example

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

LeetCode #109 Convert Sorted List to Binary Search Tree

Posted on 2018-09-06 | Post modified 2018-09-06 | In Languages , LeetCode , Python , Difficulties , Algorithms , Data Structures , Medium , Linked List , Divide and Conquer

Problem

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

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.

Example

1
2
3
4
5
6
7
8
9
Given the sorted linked list: [-10,-3,0,5,9],

One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:

0
/ \
-3 9
/ /
-10 5
Read more »

LeetCode #328 Odd Even Linked List

Posted on 2018-09-06 | Post modified 2018-09-06 | In LeetCode , Difficulties , Data Structures , Medium , Linked List

Problem

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.

You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.

Examples

1
2
Input: 1->2->3->4->5->NULL
Output: 1->3->5->2->4->NULL
1
2
Input: 2->1->3->5->6->4->7->NULL
Output: 2->3->6->7->1->5->4->NULL

Note

  • The relative order inside both the even and odd groups should remain as it was in the input.
  • The first node is considered odd, the second node even and so on …
Read more »

LeetCode #19 Remove Nth Node From End of List

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

Problem

Given a linked list, remove the n-th node from the end of list and return its head.

Example

1
2
3
Given linked list: 1->2->3->4->5, and n = 2.

After removing the second node from the end, the linked list becomes 1->2->3->5.
Read more »

Computing With Logic Quiz (3)

Posted on 2018-09-05 | Post modified 2018-09-15 | In Languages , Prolog
  1. Rewrite the following statements in formal way:

    • All real numbers have nonnegative squares. (Equivalent to Every Real number has a nonnegative square)

      $\forall X \in R, x^2 \geq 0$

    • All real numbers have squares not equal to -1. (Equivalent to No real numbers have squares equal to -1)

      $\forall X \in R, X^2 \text{ \}= -1$

    • There is a positive integer whose square is equal to itself. (Equivalent to Some positive integer equals its own square.)

      $\exist X \in Z^+ \text{ such that} X^2 = X$

Read more »

LeetCode #102 Binary Tree Level Order Traversal

Posted on 2018-09-05 | Post modified 2018-10-10 | In Languages , LeetCode , Python , Difficulties , Algorithms , Data Structures , Medium , Binary Tree , Breadth-first Search , Stack , Queue

Problem

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

Example

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

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

return its level order traversal as:

1
2
3
4
5
[
[3],
[9,20],
[15,7]
]
Read more »
1…141516…18
Liam Wang

Liam Wang

Liam's Personal Blog

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