leetcode:455. 分发饼干遗留下来的问题

class Solution:
    def findContentChildren(self, g, s):
        """
        :type g: List[int]
        :type s: List[int]
        :rtype: int
        """
        g.sort()
        s.sort()
        len1=len(g)
        len2=len(s)
        i=0
        j=0
        num=0
        while i<len1 and j<len2:
            if g[i]<=s[j]:
                num+=1
                i+=1
            j+=1
        return num

这段代码就显示通过。而下面这段讲j+=1加入到if中就会变成超时,这是为什么?

class Solution:
    def findContentChildren(self, g, s):
        """
        :type g: List[int]
        :type s: List[int]
        :rtype: int
        """
        g.sort()
        s.sort()
        len1=len(g)
        len2=len(s)
        i=0
        j=0
        num=0
        while i<len1 and j<len2:
            if g[i]<=s[j]:
                num+=1
                i+=1
           	    j+=1
        return num

猜你喜欢

转载自blog.csdn.net/qq_37002901/article/details/82834100