Loading... 2023.12.30,我正式下定决心,打算好好的跟算法说道说道。 虽然我本人真正的与算法无缘,但,未来需要嘛! 就开个小坑,哈哈。 <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-4cafe932376c3b30dd4e64282b82dcee11" aria-expanded="true"><div class="accordion-toggle"><span style="">23-12月</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-4cafe932376c3b30dd4e64282b82dcee11" class="collapse collapse-content"><p></p> # 23-12月 ## [1185. 一周中的第几天](https://leetcode.cn/problems/day-of-the-week/) ### 解法一: 直接使用python 的 `datetime` ```python from datetime import datetime class Solution: def dayOfTheWeek(self, day: int, month: int, year: int) -> str: return datetime.datetime(year, month, day).strftime("%A") #date_object = datetime.strptime(str(year)+'-'+str(month)+'-'+str(day), "%Y-%m-%d") #day_of_week = date_object.strftime("%A") #return day_of_week ``` ### 解法二 使用蔡勒公式进行计算: $$ w = \left( \left\lfloor \frac{c}{4} \right\rfloor - 2c + y + \left\lfloor \frac{y}{4} \right\rfloor + \left\lfloor \frac{13(m+1)}{5} \right\rfloor + d - 1 \right) \mod 7 $$ - w: 星期(从 Sunday 开始) - c: 年份前两位y: 年份后两位 - m: 月(m 的取值范围是 3 至 14,即在蔡勒公式中,某年的 1、2 月要看作上一年的 13、14 月来计算,比如 2003 年 1 月 1 日要看作 2002 年的 13 月 1 日来计算) - d: 日 - ⌊⌋: 向下取整 - mod: 取余 ```python class Solution: def dayOfTheWeek(self, d: int, m: int, y: int) -> str: if m < 3: m += 12 y -= 1 c = y // 100 y = y % 100 w = (c // 4 - 2 * c + y + y // 4 + 13 * (m + 1) // 5 + d - 1) % 7 return [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", ][w] 作者:ylb 链接:https://leetcode.cn/problems/day-of-the-week/solutions/2584790/python3javacgotypescript-yi-ti-yi-jie-ca-faya/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 ``` ## [26. 删除有序数组中的重复项](https://leetcode.cn/problems/remove-duplicates-from-sorted-array/) 对于仅仅学过Python的人来说,代码应该是这样的,当然这不是最Python的写法啦 ```python class Solution: def removeDuplicates(self, nums: List[int]) -> int: k=len(list(set(nums))) col=list(set(nums)) nums[:k] = sorted(col) return k ``` 但是,对于学过算法的,应该是这样写的: 这里我参考了力扣官方的写法,emmm,确实如评论区所说,有点晦涩难懂。(不如自己再写一个) 当然,看懂的话,其实都一样 ```python class Solution: def removeDuplicates(self, nums: List[int]) -> int: if not nums: return 0 n = len(nums) fast = slow = 1 while fast < n: if nums[fast] != nums[fast - 1]: nums[slow] = nums[fast] slow += 1 fast += 1 return slow ``` 快慢指针的想法就是 假设是有序的,快指针快速找到不重复的元素,慢指针指向的是替代的位置。 因为最近在学c语言,所以这里也贴一下: ```c int removeDuplicates(int* nums, int numsSize) { if (numsSize == 0) { return 0; } int fast = 1, slow = 1; while (fast < numsSize) { if (nums[fast] != nums[fast - 1]) { nums[slow] = nums[fast]; ++slow; } ++fast; } return slow; } ``` ## [1154. 一年中的第几天](https://leetcode.cn/problems/day-of-the-year/) 对于学过datetime的,我们可以直接使用datetime模块轻松的得到答案: ```python from datetime import datetime class Solution: def dayOfYear(self, date: str) -> int: return int(datetime.strptime(date, "%Y-%m-%d").strftime("%j")) ``` 这是参考文档:[https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior) 当然,还有另外一种写法是这样的: ```python from datetime import datetime class Solution: def dayOfYear(self, date: str) -> int: date_object = datetime.strptime(date, "%Y-%m-%d") day_of_year = date_object.timetuple().tm_yday return day_of_year ``` 下面是用算法的角度来实现的: 首先我们要对str按照-进行划分,解析出年月日 除此之外我们需要知道:闰年?每月的天数? 这样我们就可以进行sum,就得到这个日期在一年之中是第几天了: ```python class Solution: def dayOfYear(self, date: str) -> int: year, month, day = [int(x) for x in date.split("-")] amount = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0): amount[1] += 1 ans = sum(amount[:month - 1]) return ans + day 作者:力扣官方题解 链接:https://leetcode.cn/problems/day-of-the-year/solutions/1167809/yi-nian-zhong-de-di-ji-tian-by-leetcode-2i0gr/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 ``` <p></p></div></div></div> ## [1599. 经营摩天轮的最大利润](https://leetcode.cn/problems/maximum-profit-of-operating-a-centennial-wheel/) 题目就是一个模拟过程 废话少说,直接上代码 ```python class Solution: def minOperationsMaxProfit(self, customers: List[int], boardingCost: int, runningCost: int) -> int: ans=-1 m=t=0 i=0 wait=0 while wait or i < len(customers): wait+=customers[i] if i < len(customers) else 0 up=min(wait,4) wait-=up t+=up*boardingCost-runningCost i+=1 if t>m: m=t ans=i return ans ``` 说明: wait是等待的游客数目,i是转动次数,up是当前上去的游客数目,t是当前的利润,m是最大利润,ans是最后要返回的操作次数。 ## [2807. 在链表中插入最大公约数](https://leetcode.cn/problems/insert-greatest-common-divisors-in-linked-list/) 对于最大公约数,我们使用 `gcd`函数得到,思想方法是辗转相除法 ```python def gcd(a, b): while b: a, b = b, a % b return a ``` ```python class Solution: def insertGreatestCommonDivisors(self, head: Optional[ListNode]) -> Optional[ListNode]: cur = head while cur.next: cur.next = ListNode(gcd(cur.val, cur.next.val), cur.next) cur = cur.next.next return head ``` 我们需要再cur的后面插入我们的最大公约数节点, val就是gcd的返回内容,next应该是cur的next 然后我们把cur移动两个。 ## [383. 赎金信](https://leetcode.cn/problems/ransom-note/) 人生苦短,我用python,一行就可以解决了 我们使用collections的Counter,这个玩意可以帮我们计算一个可迭代对象的可哈希玩意出现的次数 ```python from collections import Counter class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: return Counter(ransomNote) <= Counter(magazine) ``` Last modification:January 7, 2024 © Allow specification reprint Support Appreciate the author AliPayWeChat Like 如果觉得我的内容对你有用,请随意赞赏
One comment
%%%%%