第111场双周赛

发布时间 2023-08-19 23:46:35作者: YuhangL

(应该减8min 问就是洗澡去了 38开始做的)

 四个字:浅尝辄止

因为只能浅尝辄止

 

第一题:

 

思路 两个遍历

class Solution(object):
    def countPairs(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        n=len(nums)
        ans=0
        for i in range(0,n-1):
            for j in range(i+1,n):
                if nums[i]+nums[j]<target:
                    ans=ans+1
        return ans

 

 第一遍WA了 ? 因为索引问题 取不到最后一个元素

第二遍改过来就AC了 

 

第二题:

 

 就是一个plus版的判断子序列问题

class Solution(object):
    def canMakeSubsequence(self, str1, str2):
        """
        :type str1: str
        :type str2: str
        :rtype: bool
        """
        len_1=len(str1)
        len_2=len(str2)
        i=j=0
        while i<len_1 and j <len_2:
            if str1[i]==str2[j] or  (str1[i]=='z' and str2[j]=='a') or ord(str1[i])-ord(str2[j])==-1:
                j=j+1
            i=i+1
        return (j==len_2)
            
        

第一遍 WA 是Because

if str1[i]==str2[j] or (str1[i]=='a' and str2[j]=='z') or (str1[i]=='z' and str2[j]=='a') or abs(ord(str1[i])-ord(str2[j]))==1:

这个判断条件是str1可以增可以减 不是题目所说的增

第二遍WA 发现这个问题了但是没完全发现

if str1[i]==str2[j] or (str1[i]=='a' and str2[j]=='z') or (str1[i]=='z' and str2[j]=='a') or ord(str1[i])-ord(str2[j])==-1:

第三遍WA 这个时候以为已经解决了减的问题了 于是加了一个关于长度的判断

        len_1=len(str1)
        len_2=len(str2)
        if len_1<len_2:
            return false
        i=j=0
        while i<len_1 and j <len_2:
            if str1[i]==str2[j] or (str1[i]=='a' and str2[j]=='z') or (str1[i]=='z' and str2[j]=='a') or ord(str1[i])-ord(str2[j])==-1:
                j=j+1
            i=i+1
        return (j==len_2)

第四遍 Runtime error

彻底解决了减的问题 str1的a是不能变成z的 这是减  关于长度的判断还保留

if str1[i]==str2[j] or  (str1[i]=='z' and str2[j]=='a') or ord(str1[i])-ord(str2[j])==-1:

因为把False写成false是false的 又戳啦?

第五遍终于过了

第三题:

 看着就不想做

第四题:

 思路是暴力遍历解决

class Solution(object):
    def check_jiou(self,num):
        ji=0
        ou=0
        for ch in str(num):
            if int(ch)%2==0:
                ou=ou+1
            else :
                ji=ji+1
        return (ji==ou)
    def check_chu(self,num,k):
        if num%k==0:
            return True
        else:
            return False
    def numberOfBeautifulIntegers(self, low, high, k):
        """
        :type low: int
        :type high: int
        :type k: int
        :rtype: int
        """
        ans=0
        for i in range(low,high+1):
            if self.check_chu(i,k) and self.check_jiou(i):
                ans=ans+1
        return ans

但是不行 后续做了一些改进 还是不行

 怪不得是最后一道题

 

就到这里啦 明天起得来的话就做明天的周赛