T1 LC983 最低票价

动态规划,倒序生成,需要使用@lru_cache(None)

  1. class Solution:
  2. def mincostTickets(self, days: List[int], costs: List[int]) -> int:
  3. duration =[1,7,30]
  4. @lru_cache(None)
  5. def dp(i):
  6. if i>365:return 0
  7. elif i in days:
  8. return min(dp(i+d)+c for c,d in zip(costs,duration))
  9. else:return dp(i+1)
  10. return dp(1)

T2 LC1290 二进制链表转整数

  1. class Solution {
  2. public:
  3. int getDecimalValue(ListNode* head) {
  4. long long result=0;
  5. while (head) {
  6. result = result * 2 + head->val;
  7. head = head->next;
  8. }
  9. return result;
  10. }
  11. };

T3 LC468 验证IP地址

使用基础函数库加正则表达式

  1. import re
  2. class Solution:
  3. chunk_IPv4 = r'([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
  4. patten_IPv4 = re.compile(r'^(' + chunk_IPv4 + r'\.){3}' + chunk_IPv4 + r'$')
  5. chunk_IPv6 = r'([0-9a-fA-F]{1,4})'
  6. patten_IPv6 = re.compile(r'^(' + chunk_IPv6 + r'\:){7}' + chunk_IPv6 + r'$')
  7. def validIPAddress(self, IP: str) -> str:
  8. if '.' in IP:
  9. return "IPv4" if self.patten_IPv4.match(IP) else "Neither"
  10. if ':' in IP:
  11. return "IPv6" if self.patten_IPv6.match(IP) else "Neither"
  12. return "Neither"

今日学习总结

完成计网作业

完成托福练习