LeetCode 每日一题 2022/4/11-2022/4/17

这篇博客涵盖了多种算法和数据结构的应用,包括统计数字的不重复个数、字符串的行数计算、O(1)时间复杂度的集合操作、最大回文数乘积的寻找以及最常见的单词统计。文章通过实例展示了如何实现这些操作,并提供了详细的代码实现,适合进阶学习者深入理解算法和数据结构。
摘要由CSDN通过智能技术生成

记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步




4/11 357. 统计各位数字都不同的数字个数

f(n)
n=0 时 0<=x<1 只有0一种 f(0)=1
n=1 时 0<=x<10 在0位的基础上可以接1~9 9种 加上f(0) f(1)=f(0)+9
n=2 时 0<=x<10 在1位的基础上可以接0~9中的9种 f(2) = f(1) + 9*9
n=3 时可以接8种 以此类推


def countNumbersWithUniqueDigits(n):
    """
    :type n: int
    :rtype: int
    """
    if n==0:
        return 1
    if n==1:
        return 10
    ans = 10
    cur = 9
    for i in range(2,n+1):
        cur *= 11-i
        ans +=cur
    return ans
    


4/12 806. 写字符串需要的行数

line记录当前行数 l记录当前行长度
如果长度超过100 另起一行

def numberOfLines(widths, s):
    """
    :type widths: List[int]
    :type s: str
    :rtype: List[int]
    """
    line = 1
    l = 0
    for c in s:
        w = widths[ord(c)-ord["a"]]
        if l+w>100:
            line+=1
            l = w
        else:
            l+=w
    return [line,l]

4/13 380. O(1) 时间插入、删除和获取随机元素

使用list存放元素
使用map记录元素及其在list中的位置
删除元素时 找到其位置loc 将该位置需要删除的值与list尾部值交换 list减小1

import random
class RandomizedSet(object):

    def __init__(self):
        self.l=[]
        self.m = {}


    def insert(self, val):
        """
        :type val: int
        :rtype: bool
        """
        if self.m.get(val,-1)==-1:
            self.m[val] = len(self.l)
            self.l.append(val)
            return True
        else:
            return False


    def remove(self, val):
        """
        :type val: int
        :rtype: bool
        """
        if self.m.get(val,-1)>-1:
            loc = self.m[val]
            self.l[loc] = self.l[-1]
            self.l = self.l[:-1]
            self.m[val] = -1
            self.m[self.l[loc]]=loc
            return True
        else:
            return False


    def getRandom(self):
        """
        :rtype: int
        """
        return random.choice(self.l)

4/14 1672. 最富有客户的资产总量

在网格中统计每个客户资产总量

def maximumWealth(accounts):
    """
    :type accounts: List[List[int]]
    :rtype: int
    """
    ans = 0
    for l in accounts:
        ans = max(ans,sum(l))
    return ans

4/15 385. 迷你语法分析器

dfs
一个NextedInteger(NI) 只能包含一个整数 或者一个包含NI的列表
从左至右遍历s
如果第一个是[ 说明这个待解析的NI是一个列表
下一位开始是一个新的NI 调用函数解析 直至遇到]结束这个NI
否则 这个NI是一个整数 处理开头可能存在的负号
返回NI

def deserialize(s):
    """
    :type s: str
    :rtype: NestedInteger
    """
    global loc
    loc = 0
    def dfs():
        global loc
        if s[loc]=='[':
            loc+=1
            ni = NestedInteger()
            while s[loc]!=']':
                net.add(dfs())
                if s[loc]==',':
                    loc+=1
            loc+=1
            return ni
        else:
            neg = False
            if s[loc]=='-':
                neg = True
                loc+=1
            num = 0
            while loc<len(s) and s[loc].isdigit():
                num*=10
                num += int(s[loc])
                loc +=1
            if neg:
                num = -num
            return NestedInteger(num)
    return dfs()

4/16 479. 最大回文数乘积

因为n<=8 从大到小枚举回文数
因为回文只需确定左半部分即可 最长为2n位 回文数为num
判断回文数是否可以分解为两个n位整数
长度2n的回文数必定可以被11整除
从10**n-1开始找能被11整除的因数

def largestPalindrome(n):
    """
    :type n: int
    :rtype: int
    """
    MOD = 1337
    if n==1:
        return 9
    hi = 10**n-1
    lo = 10**(n-1)
    for left in range(hi,hi//10,-1):
        num,tmp = left,left
        while tmp:
            num = num*10+tmp%10
            tmp //=10
        tmp = hi//11*11
        for x in range(tmp,lo-1,-11):
            if num%x==0 and lo<=num//x<=hi:
                return num%MOD
            if num//x>hi:
                break

4/17 819. 最常见的单词

将段落小写 用空格替换所有标点符号
按空格切分单次 如果不为空的为一个单词
如果在禁用列表 跳过
在map中增加出现次数 记录最多次数

def mostCommonWord(paragraph, banned):
    """
    :type paragraph: str
    :type banned: List[str]
    :rtype: str
    """
    paragraph = paragraph.lower()
    for punc in ["!","'","?",";",",","."]: #!?',;.
        paragraph = paragraph.replace(punc," ")
    ban = set(banned)
    l = paragraph.split(" ")
    m={}
    ans = ""
    maxnum = 0
    for word in l:
        if word=="":
            continue
        if word in ban:
            continue
        m[word] = m.get(word,0)+1
        if m[word]>maxnum:
            maxnum=m[word]
            ans = word
    return ans

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值