LeetCode 周赛 254

August 16, 2021

第一题 - 作为子字符串出现在单词中的字符串数目

简单
原题链接:https://leetcode-cn.com/problems/number-of-strings-that-appear-as-substrings-in-word

题目描述

给你一个字符串数组 patterns 和一个字符串 word ,统计 patterns 中有多少个字符串是 word 的子字符串。返回字符串数目。
子字符串 是字符串中的一个连续字符序列。

Python题解

class Solution(object):
    def numOfStrings(self, patterns, word):
        """
        :type patterns: List[str]
        :type word: str
        :rtype: int
        """
        ans = 0
        for pattern in patterns:
            if pattern in word:
                ans += 1

        return anspython

总结

in 关键字实属开挂行为,高阶做法参考官解。

第二题 - 构造元素不等于两相邻元素平均值的数组

中等
原题链接:https://leetcode-cn.com/problems/array-with-elements-not-equal-to-average-of-neighbors

题目描述

给你一个 下标从 0 开始 的数组 nums ,数组由若干 互不相同的 整数组成。你打算重新排列数组中的元素以满足:重排后,数组中的每个元素都 不等于 其两侧相邻元素的 平均值 。
更公式化的说法是,重新排列的数组应当满足这一属性:对于范围 1 <= i < nums.length - 1 中的每个 i ,(nums[i-1] + nums[i+1]) / 2 不等于 nums[i] 均成立 。
返回满足题意的任一重排结果。

Python题解

class Solution(object):
    def rearrangeArray(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        for i in range(1, len(nums) - 1):
            if nums[i] == (nums[i + 1] + nums[i - 1]) / 2:
                tmp = nums[i]
                nums[i] = nums[-1]
                nums[-1] = tmp
        return numspython

总结

没看官解,过了就行

第三题 - 数组元素的最小非零乘积

中等
原题链接:https://leetcode-cn.com/problems/minimum-non-zero-product-of-the-array-elements

题目描述

给你一个正整数 p 。你有一个下标从 1 开始的数组 nums ,这个数组包含范围 [1, 2p - 1] 内所有整数的二进制形式(两端都 包含)。你可以进行以下操作 任意 次:
从 nums 中选择两个元素 x 和 y  。 选择 x 中的一位与 y 对应位置的位交换。对应位置指的是两个整数 相同位置 的二进制位。 比方说,如果 x = 1101 且 y = 0011 ,交换右边数起第 2 位后,我们得到 x = 1111 和 y = 0001 。
请你算出进行以上操作 任意次 以后,nums 能得到的 最小非零 乘积。将乘积对 109 + 7 取余 后返回。
注意:答案应为取余 之前 的最小值。

Python题解

没写出来,todo...

总结

todo...

第四题 - 你能穿过矩阵的最后一天

困难
原题链接:https://leetcode-cn.com/problems/last-day-where-you-can-still-cross/
题都没看 遇到困难 睡大觉 - -
The End