File tree 1 file changed +12
-6
lines changed
1 file changed +12
-6
lines changed Original file line number Diff line number Diff line change 41
41
# https://leetcode.com/problems/two-sum/
42
42
twosum = { nums , target , debug ? false } :
43
43
let
44
- h = { } ;
45
- add = ( h : k : v : h // { k = v ; } ) ;
46
44
impl = ( builtins . foldl'
47
45
( acc : num :
48
46
let
49
47
comp = target - num ;
50
48
complement = builtins . toString ( comp ) ;
51
- exists = builtins . hasAttr complement h ;
52
- compAddsToTarget = exists && comp + num == target ;
49
+ exists = builtins . hasAttr complement acc . h ;
50
+ compAddsToTarget = exists && sum == target ;
51
+ sum = comp + num ;
53
52
in
54
53
if builtins . length acc . ans > 0 then acc
55
54
else
56
55
if compAddsToTarget
57
56
then acc // {
58
- ans = [ h . ${ complement } acc . i ] ;
57
+ ans = [ acc . h . ${ complement } acc . i ] ;
59
58
detected = true ;
60
59
}
61
60
else acc // {
65
64
i = acc . i + 1 ;
66
65
nums = acc . nums ++ [ num ] ;
67
66
complements = acc . complements ++ [ complement ] ;
67
+ sums = acc . sums ++ [ sum ] ;
68
+ doesAddToTarget = acc . doesAddToTarget ++ [ compAddsToTarget ] ;
69
+ everExists = acc . everExists ++ [ exists ] ;
70
+
68
71
}
69
72
)
70
73
{
71
74
i = 0 ;
72
- h = h ;
75
+ h = { } ;
73
76
ans = [ ] ;
74
77
nums = [ ] ;
75
78
complements = [ ] ;
76
79
detected = false ;
80
+ sums = [ ] ;
81
+ doesAddToTarget = [ ] ;
82
+ everExists = [ ] ;
77
83
}
78
84
nums ) ;
79
85
in
You can’t perform that action at this time.
0 commit comments