LeetCode Algorithm - Easy 刷题记录

Python 专栏收录该内容
1 篇文章 0 订阅

8/24/2020

1480. Running Sum of 1d Array

  • 我的做法
class Solution:
    def runningSum(self, nums: List[int]) -> List[int]:
        sums = []
        sum = 0
        for num in nums:
            sum += num
            sums.append(sum)
        return sums
  • 大神做法
class Solution:
    def runningSum(self, nums: List[int]) -> List[int]:
        return accumulate(nums)   

我是用了一个简单的loop,大神直接用了itertools的函数。

itertools — 为高效循环而创建迭代器的函数
在这里插入图片描述


1119. Remove Vowels from a String

class Solution:
    def removeVowels(self, S: str) -> str:
        return S.replace("a","").replace("e","").replace("i","").replace("o","").replace("u","")
  • 大神
class Solution:
    def removeVowels(self, S: str) -> str:
        return "".join(c for c in S if c not in "aeiou")
  • 小伙伴
class Solution:
    def removeVowels(self, S: str) -> str:
        import re
        return re.sub("a|e|i|o|u","",S)

检索替换字符串
Python字符串操作之字符串搜索与替换
python使用re进行字符串查找和替换


1431. Kids With the Greatest Number of Candies

class Solution:
    def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]:
        max_num = max(candies)
        return [candy + extraCandies >= max_num for candy in candies]

布尔值


1470. Shuffle the Array

class Solution:
    def shuffle(self, nums: List[int], n: int) -> List[int]:
        result = []
        for i in range(n):
            result.append(nums[i])
            result.append(nums[i+n])
        return result

1512. Number of Good Pairs

class Solution:
    def numIdenticalPairs(self, nums: List[int]) -> int:
        n = 0
        m = 0
        for num in set(nums):
            n = nums.count(num) * (nums.count(num) - 1)//2
            m += n
        return m
  • 大神
    def numIdenticalPairs(self, A):
        return sum(k * (k - 1) // 2 for k in collections.Counter(A).values())

查询List中元素出现次数
python 统计list中各个元素出现的次数

  1. 利用Python字典统计
  2. 利用Python的collection包下Counter的类统计
  3. 利用Python的pandas包下的value_counts的类统计
  4. 利用Python的str.count(“a”)

8/25/2020

1108. Defanging an IP Address

class Solution:
    def defangIPaddr(self, address: str) -> str:
        return address.replace(".","[.]")

771. Jewels and Stones

class Solution:
    def numJewelsInStones(self, J: str, S: str) -> int:
        m=0
        for i in list(J):
            n=S.count(i)
            m+=n
        return m
  • 大神
class Solution(object):
    def numJewelsInStones(self, J, S):
        return sum(s in set(J) for s in S)

1342. Number of Steps to Reduce a Number to Zero

class Solution:
    def numberOfSteps (self, num: int) -> int:
        i = 0
        while num>0:
            if num%2 == 0:
                num = num/2
            else:
                num = num-1
            i += 1
        return i

1528. Shuffle String

这道题我没写出来,菜鸟哇。

class Solution:
    def restoreString(self, s: str, indices: List[int]) -> str:
        ans = list(s)
        for i, j in zip(indices, list(s)):
            ans[i] = j
        return "".join(ans)

1365. How Many Numbers Are Smaller Than the Current Number

class Solution:
    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
        return [sorted(nums).index(a) for a in nums]

8/26/2020

1281. Subtract the Product and Sum of Digits of an Integer

class Solution:
    def subtractProductAndSum(self, n: int) -> int:
        p = 1
        s = 0
        for i in [int(item) for item in str(n)]:
            p = p*i
            s = s+i
        result = p - s
        return result

int怎么转list:

  • map(int, str(num))
  • [int(x) for x in str(num)]

1313. Decompress Run-Length Encoded List

class Solution:
    def decompressRLElist(self, nums: List[int]) -> List[int]:    
        result= []   
		# linear scan with step size = +2
        for i in range( 0, len(nums), 2):
            result.extend( [nums[i+1]] * nums[i])  
        return result

1165. Single-Row Keyboard

class Solution:
    def calculateTime(self, keyboard: str, word: str) -> int:
        idx = []
        result = 0
        last = 0
        for w in word:
            idx += [keyboard.index(w)]
        for x in idx:
            result += abs(x-last)
            last = x
        return result

1486. XOR Operation in an Array

class Solution:
    def xorOperation(self, n: int, start: int) -> int:
        result = start
        for i in range(1,n):
            num = start + 2*i
            result = result ^ num
        return result

1389. Create Target Array in the Given Order

class Solution:
    def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
        ans = []
        for i,j in zip(index, nums):
            ans.insert(i, j)
        return ans
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:深蓝海洋 设计师:CSDN官方博客 返回首页

打赏作者

Yating260

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值