leetcode

 from https://leetcode.com/problems/intersection-of-two-arrays/

第一題:

Intersection of Two Arrays

Given two integer arrays nums1 and nums2, 

return an array of their intersection. 

Each element in the result must be unique and you may return the result in any order.


Example 1:


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

Output: [2]

Example 2:


Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]

Output: [9,4]

Explanation: [4,9] is also accepted.


Constraints:


1 <= nums1.length, nums2.length <= 1000

0 <= nums1[i], nums2[i] <= 1000


    Public Shared Function Question1(objNum1() As Integer, objNum2() As Integer) As Integer()

        Dim tmp = New Hashtable()

        For i As Integer = 0 To objNum1.Length - 1

            If objNum2.Contains(objNum1(i)) Then

                If Not tmp.ContainsKey(objNum1(i)) Then

                    tmp.Add(objNum1(i), objNum1(i))

                End If

            End If

        Next i

        Return tmp.Keys.Cast(Of Integer)().ToArray()

    End Function


from https://leetcode.com/problems/move-zeroes/

第二題:

Given an integer array nums, 

move all 0's to the end of it while maintaining the relative order of the non-zero elements.


Note that you must do this in-place without making a copy of the array.


Example 1:


Input: nums = [0,1,0,3,12]

Output: [1,3,12,0,0]

Example 2:


Input: nums = [0]

Output: [0]

 

Constraints:


1 <= nums.length <= 104

-231 <= nums[i] <= 231 - 1

 

Follow up: Could you minimize the total number of operations done?


    Public Shared Function Question2(objNum1() As Integer) As Integer()

        For i As Integer = 1 To objNum1.Length - 1

            Dim checkIndex = objNum1.Length - 1 - i

            If objNum1(checkIndex) = 0 Then

                '此數移到最後,此數之後的數往前移

                For j As Integer = checkIndex To objNum1.Length - 2

                    objNum1(j) = objNum1(j + 1)

                Next j

                objNum1(objNum1.Length - 1) = 0

            End If

        Next i

        Return objNum1

    End Function



from https://leetcode.com/problems/k-diff-pairs-in-an-array/

第三題:

Given an array of integers nums and an integer k, 

return the number of unique k-diff pairs in the array.


A k-diff pair is an integer pair (nums[i], nums[j]), where the following are true:


0 <= i, j < nums.length

i != j

nums[i] - nums[j] == k

Notice that |val| denotes the absolute value of val.


Example 1:


Input: nums = [3,1,4,1,5], k = 2

Output: 2

Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5).

Although we have two 1s in the input, we should only return the number of unique pairs.

Example 2:


Input: nums = [1,2,3,4,5], k = 1

Output: 4

Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).

Example 3:


Input: nums = [1,3,1,5,4], k = 0

Output: 1

Explanation: There is one 0-diff pair in the array, (1, 1).

 

Constraints:


1 <= nums.length <= 104

-107 <= nums[i] <= 107

0 <= k <= 107


    Public Shared Function Question3(objNum1() As Integer, k As Integer) As Integer

        Dim count = 0

        'remove repeat value

        Dim tmp = New Hashtable()

        For i As Integer = 0 To objNum1.Length - 1

            If Not tmp.ContainsKey(objNum1(i)) Then

                tmp.Add(objNum1(i), objNum1(i))

            End If

        Next i

        objNum1 = tmp.Keys.Cast(Of Integer)().ToArray()


        For i As Integer = 0 To objNum1.Length - 2

            For j As Integer = i + 1 To objNum1.Length - 1

                Dim diff = Math.Abs(objNum1(i) - objNum1(j))

                If diff = k Then

                    count = count + 1

                End If

            Next j

        Next i

        Return count

    End Function



留言

熱門文章