Leetcode 算法题06

来源:互联网 发布:天苍苍野茫茫网络歌手 编辑:程序博客网 时间:2024/05/16 15:38

吴恩达的deeplearning第四节出中文字幕了,可能每天的解题量不会那么多了,找个时间把之前跳过关于树的题也做了

如果自己英语能再好一点就好了,会省很多时间,但是目前来看想尽快学会就找好中文资料再学,死磕英文太慢了

122. Best Time to Buy and Sell Stock II

自由发挥题,给出一个股票每天的股价,求最多能赚的钱

我的代码:

class Solution(object):    def maxProfit(self, prices):        """        :type prices: List[int]        :rtype: int        """        ans = 0        for i in range(len(prices)-1):            ans += max(0,prices[i+1]-prices[i])        return ans

453. Minimum Moves to Equal Array Elements

输入由n个数字组成的列表,每次将其中n-1个数字同时+1,求最少多少次后这个列表中每个数字相同

我的代码:同时n-1个数字+1  和每次只有一个数字 -1等价

class Solution(object):    def minMoves(self, nums):        """        :type nums: List[int]        :rtype: int        """        ans = 0        minus = min(nums)          for i in nums:            ans += abs(i-minus)        return ans
大神的代码:思路是一样的,代码更好

class Solution(object):    def minMoves(self, nums):        """        :type nums: List[int]        :rtype: int        """        return sum(nums) - len(nums)*min(nums)

349. Intersection of Two Arrays
求两个列表交集,每个数只能出现一次

我的代码:

class Solution(object):    def intersection(self, nums1, nums2):        """        :type nums1: List[int]        :type nums2: List[int]        :rtype: List[int]        """        a=list(set(nums1))        b=list(set(nums2))        ans = []        for i in a:            if i in b:                ans.append(i)        return ans
大神的代码:对啊!set可以直接用&操作,一时糊涂啊

class Solution(object):def intersection(self, nums1, nums2):    """    :type nums1: List[int]    :type nums2: List[int]    :rtype: List[int]    """    nums1=set(nums1)    nums2=set(nums2)    return list(nums1&nums2)


383. Ransom Note
给两个字符串,输出第一个是否能被另一个里的字母构成,数量也要对上

我的代码:

class Solution(object):    def canConstruct(self, ransomNote, magazine):        """        :type ransomNote: str        :type magazine: str        :rtype: bool        """        a = collections.Counter(ransomNote)        b = collections.Counter(magazine)        for i in a:            if i in b:                if a[i] > b[i]:                    return False            else:                return False        return True
大神的代码:原来Counter类型可以直接用减法操作。。看来是个很有用的函数

def canConstruct(self, ransomNote, magazine):    return not collections.Counter(ransomNote) - collections.Counter(magazine)

455. Assign Cookies:

分饼干问题,输入每个孩子要求的质量和每块饼干的质量,每个孩子最多拿一块饼干,求最多能满足多少个孩子

第一次尝试超时了:

class Solution(object):    def findContentChildren(self, g, s):        """        :type g: List[int]        :type s: List[int]        :rtype: int        """        g.sort()        s.sort()        count = 0        while s and count < len(g) and g[count] <= s[-1]:            for i in range(len(s)):                if s[i] >= g[count]:                    count += 1                    s=s[i+1:]                    break        return count       
改进版:中间想了个别的思路写了一个小时测试,最后发现思路是错的,还是要回到这个思路,中间一直做不对挺烦躁的,休息了一下就一下做出来了,唉

其实如果能确定思路是对的,能考虑所有可能,就沿着这个思路想就好了,重新换思路又要考虑所有可能

class Solution(object):    def findContentChildren(self, g, s):        """        :type g: List[int]        :type s: List[int]        :rtype: int        """        g.sort()        s.sort()        if g == [] or s == [] or g[0] > s[-1]:            return 0        count = 0        while s and count < len(g):            if s[0] >= g[count]:                count += 1            s.pop(0)        return count

大神的代码:

class Solution(object):    def majorityElement(self, nums):        """        :type nums: List[int]        :rtype: int        """        return sorted(nums)[len(nums)/2]







原创粉丝点击