Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
李楚霏
/
leetCode_Solution_Record
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
5c9ccb7e
authored
Aug 25, 2020
by
李楚霏
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
day-1三数之和
parent
0349e630
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
0 deletions
+46
-0
70works/day1_threeSum.js
+46
-0
No files found.
70works/day1_threeSum.js
0 → 100644
View file @
5c9ccb7e
/*
* @Author: your name
* @Date: 2020-08-24 23:17:23
* @LastEditTime: 2020-08-25 01:14:49
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \70works\day1_threeSum.js
*/
/**
* @param {number[]} nums
* @return {number[][]}
*/
var
threeSum
=
function
(
nums
)
{
let
res
=
[];
let
len
=
nums
.
length
;
nums
.
sort
((
a
,
b
)
=>
a
-
b
);
// leetcode上不能直接return,而要具体返回一个对象才行;
//否则会报错;输入为空,输出为undefined;
if
(
len
<
0
||
len
<
3
)
return
res
;
for
(
let
i
=
0
;
i
<
len
;
i
++
)
{
let
L
=
i
+
1
;
let
R
=
len
-
1
;
if
(
nums
[
i
]
>
0
)
break
;
if
(
i
>
0
&&
nums
[
i
]
==
nums
[
i
-
1
])
continue
;
while
(
L
<
R
)
{
const
sum
=
nums
[
i
]
+
nums
[
L
]
+
nums
[
R
];
if
(
sum
===
0
)
{
res
.
push
([
nums
[
i
],
nums
[
L
],
nums
[
R
]]);
while
(
L
<
R
&&
nums
[
L
]
==
nums
[
L
+
1
])
L
++
;
while
(
L
<
R
&&
nums
[
R
]
==
nums
[
R
+
1
])
R
--
;
L
++
;
R
--
;
}
else
if
(
sum
<
0
){
L
++
;
}
else
if
(
sum
>
0
){
R
--
;
}
}
}
return
res
;
};
let
nums
=
[
-
1
,
0
,
1
,
2
,
-
1
,
-
4
]
threeSum
(
nums
);
// module.exports = threeSum;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment