当前位置: 首页> 娱乐> 八卦 > 直销公司业绩排名_深圳燃气小程序_免费网站制作app_搜索关键词的网站

直销公司业绩排名_深圳燃气小程序_免费网站制作app_搜索关键词的网站

时间:2025/8/8 16:50:41来源:https://blog.csdn.net/LIEBESFREUD/article/details/144819177 浏览次数:0次
直销公司业绩排名_深圳燃气小程序_免费网站制作app_搜索关键词的网站

242. 有效的字母异位词

哈希表有最大长度26,直接使用数组做哈希。

class Solution:def isAnagram(self, s: str, t: str) -> bool:record = [0 for i in range(26)]for char in s:record[ord(char)-ord('a')] += 1for char in t:record[ord(char)-ord('a')] -= 1return not any(record)

349. 两个数组的交集

这题python其实很好AC一行就可以了..

class Solution:def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:return list(set(nums1) & set(nums2))

学一下别人的写法:

class Solution:def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:set1 = set(nums1)set2 = set(nums2)return self.set_intersection(set1, set2)def set_intersection(self, set1, set2):if len(set1) > len(set2):return self.set_intersection(set2, set1)return [x for x in set1 if x in set2]

定义交集函数让set1总是更小的可以优化性能。

202. 快乐数

 建一个集合用于记录目前为止的各位平方和,如果等于1说明是快乐数,否则一旦发现重复了直接报告Flase.

class Solution:def isHappy(self, n: int) -> bool:record = set()while True:record.add(n)n = sum([int(x) ** 2 for x in str(n)])if n == 1:return Trueif n in record:return False

1. 两数之和

每次做这一题都需要打开评论查看一下"有人相爱,有人夜里开车看海,有人leetcode第一题都做不出来。"

言归正传,建一个哈希表,key放数值,value放对应的索引。每次循环查target-num键是否在哈希表中,如果在报告结果。否则把当前num存入哈希表,值为对应的索引。

from collections import defaultdict
class Solution:def twoSum(self, nums: List[int], target: int) -> List[int]:record = defaultdict(int)for i,num in enumerate(nums):if target - num in record:return [i, record[target - num]]record[num] = ireturn []

这里再写一个双指针实现,为后续的三数之和、四数之和做准备。

class Solution:def twoSum(self, nums: List[int], target: int) -> List[int]:sorted_nums = sorted(nums)left, right = 0, len(nums) - 1while left < right:if sorted_nums[left] + sorted_nums[right] == target:left_index = nums.index(sorted_nums[left])right_index =  nums.index(sorted_nums[right])return [left_index, nums[left_index + 1:].index(sorted_nums[left]) + left_index + 1] if sorted_nums[left] == sorted_nums[right] else [left_index, right_index]elif sorted_nums[left] + sorted_nums[right] < target:left += 1else:right -= 1

关键字:直销公司业绩排名_深圳燃气小程序_免费网站制作app_搜索关键词的网站

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: