242. 有效的字母异位词,349. 两个数组的交集 ,202. 快乐数,1. 两数之和
242. 有效的字母异位词
给定两个字符串 s s s 和 t t t ,编写一个函数来判断 t t t 是否是 s s s 的 字母异位词。
示例 1:
输入: s = “anagram”, t = “nagaram”
输出: true
示例 2:
输入: s = “rat”, t = “car”
输出: false
题目中字符串只有小写字符所以字符唯一,简单思路, s s s, t t t分别建立字典哈希表,索引为字符当前字符,索引值为出现次数,判断两个字典是否相等。
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
res1 =dict()
res2 =dict()
for i in s:
if i not in res1:
res1[i] = 1
else:
res1[i] += 1
for i in t:
if i not in res2:
res2[i] = 1
else:
res2[i] += 1
return res1==res2
附采用其他python模块方法中的代码。
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
from collections import defaultdict
s_dict = defaultdict(int)
t_dict = defaultdict(int)
for x in s:
s_dict[x] += 1
for x in t:
t_dict[x] += 1
return s_dict == t_dict
class Solution(object):
def isAnagram(self, s: str, t: str) -> bool:
from collections import Counter
a_count = Counter(s)
b_count = Counter(t)
return a_count == b_count
思想相同,都是建立哈希表判断。可以只用一个 r e s res res 数组:把 s [ i ] s[i] s[i] 的出现次数加一,把 t [ i ] t[i] t[i] 的出现次数减一,最后判断 r e s res res 数组是否全为 0 0 0 。
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
res =dict()
for i in s:
if i not in res:
res[i] = 1
else:
res[i] += 1
for i in t:
if i not in res:
return False
else:
res[i] -= 1
for i in res:
if res[i] != 0:
return False
return True
Tips:这题只有小写字符,若包含大写字符且忽略大小写等情况时,可以考虑需要使用ASCII值。定一个数组叫做record,大小为 26 26 26。若其他情况考虑采用不同长度record。
349. 两个数组的交集
给定两个数组 nums1 和 nums2 ,返回 它们的
交集 。输出结果中的每个元素一定是 唯一 的。我们可以 不考虑输出结果的顺序 。
示例 1:
输入:nums1 = [1,2,2,1], nums2 = [2,2]
输出:[2]
示例 2:
输入:nums1 = [4,9,5], nums2 = [9,4,9,8,4]
输出:[9,4]
解释:[4,9] 也是可通过的
可以参考上题思路,用字典解决。这道题目只考虑交集,忽略了次数可以更简单用集合解决。 n u m 2 num2 num2在集合 r e s 1 res1 res1中元素添加到集合 r e s 2 res2 res2中,最后 r e s 2 res2 res2转为list类型输出。
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
res1 = set()
res2 = set()
for i in nums1:
if i not in res1:
res1.add(i)
for i in nums2:
if i in res1:
res2.add(i)
return list(res2)
参考代码,使用python自带方法。
class Solution:
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
return list(set(nums1) & set(nums2))
202. 快乐数
编写一个算法来判断一个数
n
n
n 是不是快乐数。
「快乐数」 定义为:
- 对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。
- 然后重复这个过程直到这个数变为 1 1 1,也可能是 无限循环 但始终变不到 1 1 1。
- 如果这个过程 结果为 1 1 1,那么这个数就是快乐数。
如果 n n n 是 快乐数 就返回 true ,不是,则返回 false 。
这道题关键在于循环终于条件,采用字典存储要判断的值,出现重复循环终止。
class Solution:
def isHappy(self, n: int) -> bool:
res = set()
while n not in res:
res.add(n)
cnt = 0
while n//10> 0:
cnt += (n%10)**2
n = n//10
cnt += n**2
if cnt==1:
return True
n = cnt
return True if n==1 else False
参考答案,精简版本:
class Solution:
def isHappy(self, n: int) -> bool:
seen = set()
while n != 1:
n = sum(int(i) ** 2 for i in str(n))
if n in seen:
return False
seen.add(n)
return True
在这里,
i
n
in
in判断,
s
e
e
n
seen
seen使用集合和列表存储都行,求和阶段利用字符转换精简,思想都是一样的。
参考其他人方法,使用 “快慢指针” 思想,找出循环:“快指针” 每次走两步,“慢指针” 每次走一步,当二者相等时,即为一个循环周期。此时,判断是不是因为 1 引起的循环,是的话就是快乐数,否则不是快乐数。如果集合大到无法存储,上面方法失效。
class Solution:
def isHappy(self, n: int) -> bool:
slow = n
fast = n
while self.get_sum(fast) != 1 and self.get_sum(self.get_sum(fast)):
slow = self.get_sum(slow)
fast = self.get_sum(self.get_sum(fast))
if slow == fast:
return False
return True
def get_sum(self,n: int) -> int:
new_num = 0
while n:
n, r = divmod(n, 10)
new_num += r ** 2
return new_num
1. 两数之和
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
你可以假设每种输入只会对应一个答案,并且你不能使用两次相同的元素。
你可以按任意顺序返回答案。
示例 1:
输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
示例 2:
输入:nums = [3,2,4], target = 6
输出:[1,2]
示例 3:
输入:nums = [3,3], target = 6
输出:[0,1]
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
res = dict()
for i in range(len(nums)):
res[nums[i]] = i
for i in range(len(nums)):
if (target - nums[i]) in res and res[target - nums[i]]!=i:
return [i,res[target - nums[i]]]
这道题目和上面基本一样,不考虑顺序,采用字典可以解决,字典索引为数组元素,索引值为元素下标。查找
t
a
r
g
e
t
−
n
u
m
s
[
i
]
target - nums[i]
target−nums[i]在字典中且索引不为
i
i
i,即位一对结果。
附参考精简答案
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
records = dict()
for index, value in enumerate(nums):
if target - value in records: # 遍历当前元素,并在map中寻找是否有匹配的key
return [records[target- value], index]
records[value] = index # 如果没找到匹配对,就把访问过的元素和下标加入到map中
return []