We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent aeb180a commit c18e917Copy full SHA for c18e917
leetcode/1 - twosum/two-sum.lua
@@ -0,0 +1,30 @@
1
+function TwoSumQuadradic(nums, target)
2
+ for i = 1, #nums do
3
+ for j = i + 1, #nums do
4
+ if nums[i] + nums[j] == target then
5
+ return i, j
6
+ end
7
8
9
+
10
+ return nil
11
+end
12
13
+function TwoSum(nums, target)
14
+ local seen = {}
15
16
+ for i, num in ipairs(nums) do
17
+ if seen[num] then
18
+ return seen[num], i
19
20
+ seen[target - num] = i
21
22
23
24
25
26
+local nums = {2, 15, 11, 7}
27
+local target = 9
28
29
+print(TwoSumQuadratic(nums, target))
30
+print(TwoSum(nums, target))
0 commit comments