码迷,mamicode.com
首页 > 其他好文 > 详细

19-9-30

时间:2019-09-30 19:52:21      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:insert   iss   sel   set   leetcode   app   最后一天   +=   index   

1.独一无二的出现次数:https://leetcode-cn.com/problems/unique-number-of-occurrences/

给你一个整数数组 arr,请你帮忙统计数组中每个数的出现次数。

如果每个数的出现次数都是独一无二的,就返回 true;否则返回 false

自己的方法:

 1 class Solution(object):
 2     def uniqueOccurrences(self, arr):
 3         """
 4         :type arr: List[int]
 5         :rtype: bool
 6         """
 7         count_list = list()
 8         one_arr = set(arr)
 9         for each_arr in one_arr:
10             count_list.append(arr.count(each_arr))
11         if len(count_list)==len(set(count_list)):
12             return True
13         else:
14             return False

思路:通过set来进行二次去重

收获:发现了一个好东西就是Counter,可以调用Counter生成字典 from collections import Counter 

2.分糖果

给定一个偶数长度的数组,其中不同的数字代表着不同种类的糖果,每一个数字代表一个糖果。你需要把这些糖果平均分给一个弟弟和一个妹妹。返回妹妹可以获得的最大糖果的种类数。

https://leetcode-cn.com/problems/distribute-candies/

class Solution(object):
    def distributeCandies(self, candies):
        """
        :type candies: List[int]
        :rtype: int
        """
        a = len(candies)/2
        b = len(set(candies))
        result_num = a if b>a else b
        return result_num

思路:很曲折 开始以为要统计数 后来发现偶数长度这个条件很关键

3.泰波那契序列 https://leetcode-cn.com/problems/n-th-tribonacci-number/

泰波那契序列 Tn 定义如下: 

T0 = 0, T1 = 1, T2 = 1, 且在 n >= 0 的条件下 Tn+3 = Tn + Tn+1 + Tn+2

给你整数 n,请返回第 n 个泰波那契数 Tn 的值。

class Solution(object):
    def tribonacci(self, n):
        """
        :type n: int
        :rtype: int
        """
        if n==0:
            return 0
        if n==1:
            return 1
        if n==2:
            return 1
        a = 0
        b = 1
        c = 1
        for num in range(3,n+1):
            sum = a + b + c
            a = b
            b = c
            c = sum
        return sum

思路:累加,看了别人的代码学了个缓存的,有空下来研究一哈

import functools

@functools.lru_cache(None)

4.买卖股票的最佳时机 https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/

给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。

设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        result = 0
        for i in range(1, len(prices)):
            tmp = prices[i] - prices[i - 1]
            if tmp > 0: result += tmp
        return result

思路:这个题 我没做出来TAT 看了别人的思路

单独的交易日:前一天买,今天卖,收益就是p2-p1

连续的增长:我们第一天买,最后一天卖就可以,相当于每天都在买卖pn-p1 = (p2-p1)+(p3-p2)+...+(pn-pn-1)

连续的减少:别买卖了

5.有效的字母异位词 https://leetcode-cn.com/problems/valid-anagram/

给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。

class Solution(object):
    def isAnagram(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        if len(s)!=len(t):
            return False
        s_list = list(s)
        t_list = list(t)
        s_list.sort()
        t_list.sort()
        for num in range(0,len(s)):
            if s_list[num]!=t_list[num]:
                return False
        return True

思路:自己思路就是,如果长度不一样就一定不是,然后对两个进行排序后对比

看到别人的一个思路,通过set去重复来得到这个词用的字母都是什么,然后通过对两个

进行count,如果两个count一致就是异位词

6.复写零 https://leetcode-cn.com/problems/duplicate-zeros/submissions/

给你一个长度固定的整数数组 arr,请你将该数组中出现的每个零都复写一遍,并将其余的元素向右平移。

注意:请不要在超过该数组长度的位置写入元素。

要求:请对输入的数组 就地 进行上述修改,不要从函数返回任何东西。

class Solution(object):
    def duplicateZeros(self, arr):
        """
        :type arr: List[int]
        :rtype: None Do not return anything, modify arr in-place instead.
        """
        num = 0
        while num<len(arr):
            if arr[num]==0:
                arr.insert(num,0)
                arr.pop()
                num = num + 2
            else:
                num = num + 1

思路:没啥思路 是0就insert 然后pop 记得跳过下一个 因为下一个0 是你自己insert的

看了一下不用insert方法

发现index为0后,对range(index,len(arr))进行换值处理,最后index+1 = 0

7.移除元素 https://leetcode-cn.com/problems/remove-element/

给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度。

不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。

元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。

class Solution(object):
    def removeElement(self, nums, val):
        """
        :type nums: List[int]
        :type val: int
        :rtype: int
        """
        num = 0
        while num < len(nums):
            if nums[num]==val:
                nums.remove(nums[num])
            else:
                num = num + 1

思路:没啥思路 就是开始就想到了remove但是突然对remove的原理有点懵,有空复习一下

19-9-30

标签:insert   iss   sel   set   leetcode   app   最后一天   +=   index   

原文地址:https://www.cnblogs.com/newliu/p/11613905.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!