西维蜀黍的OJ Blog

🐒 Software engineer | 📷 Photographer | 👹 Urban explorer

[Notes] Hashmap

  ...


[Python] Basic Python Knowledge for Leetcode

For xs = [8, 23, 45] for x in xs: print("item #{} = {}".format(index, x)) Use the built-in function enumerate(): for idx, x in enumerate(xs): print(idx, x) Data structure List l = [] # or l = list() # add l.append(1) print(l) # 1 # for loop >>> for i, value in enumerate(['A', 'B', 'C']): ... print(i, value) ... 0 A 1 B 2 C Ref https://swsmile.info/post/python-collection-list/ Map/Dict # init thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(type(thisdict)) # init 2 thisdict = dict(name = "John", age = 36, country = "Norway") print(thisdict) # delete del tel['sape'] # dict -> list >>> tel {'jack': 4098, 'guido': 4127, 'irv': 4127} >>> list(tel) ['jack', 'guido', 'irv'] # get >>> 'Thomas' in d False # for-loop >>> d = {'a': 1, 'b': 2, 'c': 3} >>> for key in d: .   ...


[刷题] LinkedList - Leetcode - 92 Reverse Linked List II

Reverse a linked list from position m to n. Do it in one-pass.

Note: 1 ≤ mn ≤ length of list.

Example:

Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL
  ...


[刷题] Sort Array - Lintcode - 548 Intersection of Two Arrays II

Description

Given two arrays, write a function to compute their intersection.

Each element in the result should appear as many times as it shows in both arrays.The result can be in any order.

Example

Example1

Input: 
nums1 = [1, 2, 2, 1], nums2 = [2, 2]
Output: 
[2, 2]

Example2

Input: 
nums1 = [1, 1, 2], nums2 = [1]
Output: 
[1]

Challenge

  • What if the given array is already sorted? How would you optimize your algorithm?
  • What if nums1’s size is small compared to num2’s size? Which algorithm is better?
  • What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
  ...


[刷题] Sort Array - Lintcode - 547 Intersection of Two Arrays

Leetcode - 349 Intersection of Two Arrays

Description

Given two arrays, write a function to compute their intersection.

Each element in the result must be unique.The order of the results needs to be ascending

Example

Example 1:

Input: nums1 = [1, 2, 2, 1], nums2 = [2, 2], 
Output: [2].

Example 2:

Input: nums1 = [1, 2], nums2 = [2], 
Output: [2].
  ...