Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfb21a57

Browse files
author
borninfreedom
committed
update赎金信,添加了使用数组作为哈希表和使用defaultdict两种写法
1 parent999bbae commitfb21a57

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

‎problems/0383.赎金信.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,52 @@ class Solution {
135135

136136
```
137137

138-
Python:
139-
```py
138+
Python写法一(使用数组作为哈希表):
139+
140+
```python
141+
classSolution:
142+
defcanConstruct(self,ransomNote:str,magazine:str) ->bool:
143+
144+
arr= [0]*26
145+
146+
for xin magazine:
147+
arr[ord(x)-ord('a')]+=1
148+
149+
for xin ransomNote:
150+
if arr[ord(x)-ord('a')]==0:
151+
returnFalse
152+
else:
153+
arr[ord(x)-ord('a')]-=1
154+
155+
returnTrue
156+
```
157+
158+
Python写法二(使用defaultdict):
159+
160+
```python
161+
classSolution:
162+
defcanConstruct(self,ransomNote:str,magazine:str) ->bool:
163+
164+
from collectionsimport defaultdict
165+
166+
hashmap= defaultdict(int)
167+
168+
for xin magazine:
169+
hashmap[x]+=1
170+
171+
for xin ransomNote:
172+
value= hashmap.get(x)
173+
if valueisNoneor value==0:
174+
returnFalse
175+
else:
176+
hashmap[x]-=1
177+
178+
returnTrue
179+
```
180+
181+
Python写法三:
182+
183+
```python
140184
classSolution(object):
141185
defcanConstruct(self,ransomNote,magazine):
142186
"""
@@ -166,6 +210,7 @@ class Solution(object):
166210
```
167211

168212
Go:
213+
169214
```go
170215
funccanConstruct(ransomNotestring,magazinestring)bool {
171216
record:=make([]int,26)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp