“This is my 36th day of participating in the First Challenge 2022. For details: First Challenge 2022”

preface

This is the first question in LeetCode’s Biweekly Contest 72, difficulty level Eazy, which examines basic manipulation of lists. In fact, the difficulty of the 72nd biweekly competition was relatively low, I solved three questions in less than half an hour, but USUALLY I can only do three questions at most.

describe

Given a 0-indexed integer array nums of length n and an integer k, return the number of pairs (i, j) where 0 <= i < j < n, such that nums[i] == nums[j] and (i * j) is divisible by k.

Example 1:

Input: nums = [3,1,2,2,2,1,3], k = 2 Output: 4 Explanation: There are 4 pairs that meet all the requirements:

  • nums[0] == nums[6], and 0 * 6 == 0, which is divisible by 2.
  • nums[2] == nums[3], and 2 * 3 == 6, which is divisible by 2.
  • nums[2] == nums[4], and 2 * 4 == 8, which is divisible by 2.
  • nums[3] == nums[4], and 3 * 4 == 12, which is divisible by 2.

Note:

  • 1 <= nums.length <= 100
  • 1 <= nums[i], k <= 100

parsing

Given an array of 0 indexed integers of length n, nums and an integer k, return (I, j) such that 0 <= I < j < n and nums[I] == nums[j] and (I * j) are divisible by k.

The maximum length of a nums is 1000, and the maximum value of the elements and k in nums is 100.

Initialize a result list result, and then double for loops through all index pairs (I,j) in nums if:

nums[i] == nums[j] and (i*j)%k==0 and [i,j] not in result 
Copy the code

Add (I,j) to result and return the length of result.

answer

class Solution(object):
    def countPairs(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        result = []
        N = len(nums)
        for i in range(N):
            for j in range(i+1, N):
                if nums[i] == nums[j] and (i*j)%k==0 and [i,j] not in result:
                    result.append([i,j])
        return len(result)
        	      
		
Copy the code

The results

237/237 Test cases passed. Status: Accepted Runtime: 858 MS Memory Usage: 13.9 MBCopy the code

The original link

Leetcode.com/contest/biw…

Your support is my biggest motivation