Skip to content

Commit 5ca2983

Browse files
committed
Update two-sum.md
1 parent a176cf4 commit 5ca2983

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

analysis/two-sum.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
###坑:
1717
我一开始用得就是hash表,但是没有考虑到下面两种情况:
18-
1. array=[3,2,4], target=6
19-
2. array=[0,2,3,0], target=0
18+
*1. array=[3,2,4], target=6
19+
*2. array=[0,2,3,0], target=0
2020
我的hash表存储的值是一个下标值,不是下标数组,没考虑相同元素出现的情况,所以对于第二种,0的下标本来有1,4,但是1被4覆盖了,导致找不到结果。然后如第一种情况,第一个元素是3,6-3也等于3,发现3在hashmap里有,下标是1,然后就输出1,1了,这是没有判断target-v不能和v是同一个位置导致的。所以找target-v时,需要排除v自己的下标,不然就相当于一个元素用了两次。
2121

2222

0 commit comments

Comments
 (0)