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

Commite1bbd30

Browse files
author
borninfreedom
committed
update 四数相加II,添加了一个更加简洁的python代码
1 parent4993e9f commite1bbd30

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

‎problems/0454.四数相加II.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class Solution {
120120

121121
Python:
122122

123-
```
123+
```python
124124
classSolution(object):
125125
deffourSumCount(self,nums1,nums2,nums3,nums4):
126126
"""
@@ -147,7 +147,31 @@ class Solution(object):
147147
if keyin hashmap:
148148
count+= hashmap[key]
149149
return count
150+
151+
# 下面这个写法更为简洁,但是表达的是同样的算法
152+
# class Solution:
153+
# def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
154+
# from collections import defaultdict
155+
156+
# hashmap = defaultdict(int)
157+
158+
# for x1 in nums1:
159+
# for x2 in nums2:
160+
# hashmap[x1+x2] += 1
161+
162+
# count=0
163+
# for x3 in nums3:
164+
# for x4 in nums4:
165+
# key = -x3-x4
166+
# value = hashmap.get(key)
167+
168+
# dict的get方法会返回None(key不存在)或者key对应的value
169+
# 所以如果value==0,就会继续执行or,count+0,否则就会直接加value
170+
# 这样就不用去写if判断了
171+
172+
# count += value or 0
150173

174+
# return count
151175

152176
```
153177

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp