leetcode基础编程:哈希表
160. 存在重复元素
难度:简单
收藏
给你一个整数数组 nums
。如果任一值在数组中出现 至少两次 ,返回 true
;如果数组中每个元素互不相同,返回 false
。
示例 1:
输入:nums = [1,2,3,1]
输出:true
- 1
- 2
示例 2:
输入:nums = [1,2,3,4]
输出:false
- 1
- 2
示例 3:
输入:nums = [1,1,1,3,3,4,3,2,4,2]
输出:true
- 1
- 2
提示:
1 <= nums.length <= 105
-109 <= nums[i] <= 109
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
if len(nums)!=len(set(nums)):
return True
else:
return False
- 1
- 2
- 3
- 4
- 5
- 6
33. 有效的数独
难度:中等
收藏
请你判断一个 9 x 9
的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。
- 数字
1-9
在每一行只能出现一次。 - 数字
1-9
在每一列只能出现一次。 - 数字
1-9
在每一个以粗实线分隔的3x3
宫内只能出现一次。(请参考示例图)
注意:
- 一个有效的数独(部分已被填充)不一定是可解的。
- 只需要根据以上规则,验证已经填入的数字是否有效即可。
- 空白格用
'.'
表示。
示例 1:
输入:board =
[["5","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
输出:true
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
示例 2:
输入:board =
[["8","3",".",".","7",".",".",".","."]
,["6",".",".","1","9","5",".",".","."]
,[".","9","8",".",".",".",".","6","."]
,["8",".",".",".","6",".",".",".","3"]
,["4",".",".","8",".","3",".",".","1"]
,["7",".",".",".","2",".",".",".","6"]
,[".","6",".",".",".",".","2","8","."]
,[".",".",".","4","1","9",".",".","5"]
,[".",".",".",".","8",".",".","7","9"]]
输出:false
解释:除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
提示:
board.length == 9
board[i].length == 9
board[i][j]
是一位数字(1-9
)或者'.'
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
st=[False for i in range(9)]
for i in range(9):
st=[False for i in range(9)]
for j in range(9):
if board[i][j]!='.':
t=eval(board[i][j])-1
if st[t]:
return False
st[t] = True
for i in range(9):
st=[False for i in range(9)]
for j in range(9):
if board[j][i]!='.':
t=eval(board[j][i])-1
if st[t]:
return False
st[t]=True
for i in range(0,9,3):
for j in range(0,9,3):
st=[False for i in range(9)]
for x in range(3):
for y in range(3):
if board[i+x][j+y]!='.':
t=eval(board[i+x][j+y])-1
if st[t]:
return False
st[t] = True
return True
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
161. 存在重复元素 II
难度:简单
收藏
给你一个整数数组 nums
和一个整数 k
,判断数组中是否存在两个 不同的索引 i
和 j
,满足 nums[i] == nums[j]
且 abs(i - j) <= k
。如果存在,返回 true
;否则,返回 false
。
示例 1:
输入:nums = [1,2,3,1], k = 3
输出:true
- 1
- 2
示例 2:
输入:nums = [1,0,1,1], k = 1
输出:true
- 1
- 2
示例 3:
输入:nums = [1,2,3,1,2,3], k = 2
输出:false
- 1
- 2
提示:
1 <= nums.length <= 10^5
-10^9 <= nums[i] <= 10^9
0 <= k <= 10^5
class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
has={}
for i in range(len(nums)):
x=nums[i]
if has and x in has.keys() and i-has[x]<=k:
return True
has[x]=i
return False
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
355. 宝石与石头
难度:简单
收藏
给你一个字符串 jewels
代表石头中宝石的类型,另有一个字符串 stones
代表你拥有的石头。 stones
中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。
字母区分大小写,因此 "a"
和 "A"
是不同类型的石头。
示例 1:
输入:jewels = "aA", stones = "aAAbbbb"
输出:3
- 1
- 2
示例 2:
输入:jewels = "z", stones = "ZZ"
输出:0
- 1
- 2
提示:
1 <= jewels.length, stones.length <= 50
jewels
和stones
仅由英文字母组成jewels
中的所有字符都是 唯一的
class Solution:
def numJewelsInStones(self, jewels: str, stones: str) -> int:
num=0
for x in stones:
if x in jewels:
num+=1
return num
- 1
- 2
- 3
- 4
- 5
- 6
- 7
\374. 子域名访问计数
难度:中等
收藏
网站域名 "discuss.leetcode.com"
由多个子域名组成。顶级域名为 "com"
,二级域名为 "leetcode.com"
,最低一级为 "discuss.leetcode.com"
。当访问域名 "discuss.leetcode.com"
时,同时也会隐式访问其父域名 "leetcode.com"
以及 "com"
。
计数配对域名 是遵循 "rep d1.d2.d3"
或 "rep d1.d2"
格式的一个域名表示,其中 rep
表示访问域名的次数,d1.d2.d3
为域名本身。
- 例如,
"9001 discuss.leetcode.com"
就是一个 计数配对域名 ,表示discuss.leetcode.com
被访问了9001
次。
给你一个 计数配对域名 组成的数组 cpdomains
,解析得到输入中每个子域名对应的 计数配对域名 ,并以数组形式返回。可以按 任意顺序 返回答案。
示例 1:
输入:cpdomains = ["9001 discuss.leetcode.com"]
输出:["9001 leetcode.com","9001 discuss.leetcode.com","9001 com"]
解释:例子中仅包含一个网站域名:"discuss.leetcode.com"。
按照前文描述,子域名 "leetcode.com" 和 "com" 都会被访问,所以它们都被访问了 9001 次。
- 1
- 2
- 3
- 4
示例 2:
输入:cpdomains = ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
输出:["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]
解释:按照前文描述,会访问 "google.mail.com" 900 次,"yahoo.com" 50 次,"intel.mail.com" 1 次,"wiki.org" 5 次。
而对于父域名,会访问 "mail.com" 900 + 1 = 901 次,"com" 900 + 50 + 1 = 951 次,和 "org" 5 次。
- 1
- 2
- 3
- 4
提示:
1 <= cpdomain.length <= 100
1 <= cpdomain[i].length <= 100
cpdomain[i]
会遵循"repi d1i.d2i.d3i"
或"repi d1i.d2i"
格式repi
是范围[1, 104]
内的一个整数d1i
、d2i
和d3i
由小写英文字母组成
class Solution:
def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
cnt={}
for str1 in cpdomains:
k=str1.find(' ')
a,b=str1.split(' ')
a=int(a)
str1=str1[k+1:]
while True:
try:
cnt[str1] +=a
except:
cnt[str1] = a
k=str1.find('.')
if k==-1:
break
str1=str1[k+1:]
res=[]
for item in cnt:
res.append(str(cnt[item])+' '+item)
return res
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
文章来源: blog.csdn.net,作者:irrationality,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_54227557/article/details/123328375
- 点赞
- 收藏
- 关注作者
评论(0)