AlphaCamp Leetcode 訓練營 03. Map & Set — 觀念入門

課程資訊

此為AlphacampLeetcode訓練營,內文僅為個人心得。

Map

JS高階語言(註1)的部分,用於Hash Map,以鍵(key)取值(value)

註1:代表該功能並非任何語言都會有,例如C、C++無Map功能

Set

用於處理「合集」概念數學運算,元素 (element) 可以是任何資料型態,所有的值都是唯一的 (unique values)

HashMap

練習題:

242. Valid Anagram

link

題目說明:

Given two strings s and t, return true if t is an anagram of s, and false otherwise.

給兩個字串s、t,如果這兩個字串互為相同字母異序詞,回傳true,反之傳false

條件:

解法:

961. N-Repeated Element in Size 2N Array

link

題目說明:

You are given an integer array nums with the following properties:

Return the element that is repeated n times.

條件:

解法:

136. Single Number

link

題目說明:

Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.

You must implement a solution with a linear runtime complexity and use only constant extra space.

條件:

解法:

80. Remove Duplicates from Sorted Array II (M)

link

題目說明:

Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same.

Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.

Return k after placing the final result in the first k slots of nums.

Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.

條件:

解法:

要求

260. Single Number III (M)

link

題目說明:

Given an integer array nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in any order.

You must write an algorithm that runs in linear runtime complexity and uses only constant extra space.

條件:

解法:

非 constant extra space.

關於程式的一些心得