16. 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
}
};
解题思路:
-
自己的解题思路
参照3Sum做的,按照我之前的思路。
有考虑过Two-Pointers Tech,但是首先想自己的先2后1,发现还是存在同样的问题。之后,再想先1后2,但是当时不知道自己怎么就把这个方法给pass掉了。然后,看了其他人的解法。用这个方法是可以解决的。
说明,自己根本就没有深入领悟Two-Pointers Tech。
此题,用了之前的笨方法。可以通过所有测试案例,但是会超时。
-
别人的解题思路
先排序,之后利用Two-Pointers Tech,可以加上二叉搜索?
学习收获:
-
深刻体会到自己的不足;
-
现在的编程速度还有待提高。整理知识点时,不宜过多。
附件:程序
1、自己的程序:
运用迭代器,进行遍历。遇到重复的,则跳过版。
但超时
int
threeSumClosest
(
vector
<
int
>&
nums
,
int
target
)
{
if
(
nums
.
size
()
<
3
)
{
return
INT_MAX
;
}
sort
(
nums
.
begin
(),
nums
.
end
());
auto
p1
=
nums
.
begin
();
auto
p2
=
p1
+
1
;
int
res
=
*
p1
+
*
p2
+
*(
p2
+
1
);
while
(
p1
<
nums
.
end
()
-
2
)
{
while
(
p2
<
nums
.
end
()
-
1
)
{
auto
p3
=
p2
+
1
;
int
twoSum
=
*
p1
+
*
p2
;
int
temp
=
twoSum
+
*
p3
;
int
temp1
=
temp
;
while
(
temp
<
target
)
{
auto
next_p3
=
p3
+
1
;
while
(
next_p3
<
nums
.
end
()
&&
(*
next_p3
==
*
p3
))
{
++
next_p3
;
}
//while4
p3
=
next_p3
;
if
(
p3
==
nums
.
end
())
{
break
;
}
temp1
=
temp
;
temp
=
twoSum
+
*
p3
;
}
//while3
if
(
temp
==
target
)
{
res
=
temp
;
break
;
}
if
(
abs
(
temp1
-
target
)
<
abs
(
temp
-
target
))
{
temp
=
temp1
;
}
if
(
abs
(
temp
-
target
)
<
abs
(
res
-
target
))
{
res
=
temp
;
}
auto
next_p2
=
p2
+
1
;
while
(
next_p2
<
nums
.
end
()
-
1
&&
(*
next_p2
==
*
p2
))
{
++
next_p2
;
}
//while3
p2
=
next_p2
;
}
//while2
if
(
res
==
target
)
{
break
;
}
auto
next_p1
=
p1
+
1
;
while
(
next_p1
<
nums
.
end
()
-
2
&&
(*
next_p1
==
*
p1
))
{
++
next_p1
;
}
//while2
p1
=
next_p1
;
p2
=
p1
+
1
;
}
//while1
return
res
;
}
看了下discuss,说不需要考虑重复,所以尝试将重复检车部分去掉。
但是,还是超时了。
没有考虑重复情况,直接++迭代器版
int
threeSumClosest
(
vector
<
int
>&
nums
,
int
target
)
{
if
(
nums
.
size
()
<
3
)
{
return
INT_MAX
;
}
sort
(
nums
.
begin
(),
nums
.
end
());
auto
p1
=
nums
.
begin
();
auto
p2
=
p1
+
1
;
int
res
=
*
p1
+
*
p2
+
*(
p2
+
1
);
while
(
p1
<
nums
.
end
()
-
2
)
{
while
(
p2
<
nums
.
end
()
-
1
)
{
auto
p3
=
p2
+
1
;
int
twoSum
=
*
p1
+
*
p2
;
int
temp
=
twoSum
+
*
p3
;
int
temp1
=
temp
;
while
(
temp
<
target
)
{
++
p3
;
if
(
p3
==
nums
.
end
())
{
break
;
}
temp1
=
temp
;
temp
=
twoSum
+
*
p3
;
}
//while3
if
(
temp
==
target
)
{
res
=
temp
;
break
;
}
if
(
abs
(
temp1
-
target
)
<
abs
(
temp
-
target
))
{
temp
=
temp1
;
}
if
(
abs
(
temp
-
target
)
<
abs
(
res
-
target
))
{
res
=
temp
;
}
++
p2
;
}
//while2
if
(
res
==
target
)
{
break
;
}
++
p1
;
p2
=
p1
+
1
;
}
//while1
return
res
;
}
2、别人的程序
int
threeSumClosest
(
vector
<
int
>&
nums
,
int
target
)
{
if
(
nums
.
size
()
<
3
)
return
0
;
int
closest
=
nums
[
0
]
+
nums
[
1
]
+
nums
[
2
];
sort
(
nums
.
begin
(),
nums
.
end
());
for
(
int
first
=
0
;
first
<
nums
.
size
()
-
2
;
++
first
)
{
if
(
first
>
0
&&
nums
[
first
]
==
nums
[
first
-
1
])
continue
;
int
second
=
first
+
1
;
int
third
=
nums
.
size
()
-
1
;
while
(
second
<
third
)
{
int
curSum
=
nums
[
first
]
+
nums
[
second
]
+
nums
[
third
];
if
(
curSum
==
target
)
return
curSum
;
if
(
abs
(
target
-
curSum
)
<
abs
(
target
-
closest
))
{
closest
=
curSum
;
}
if
(
curSum
>
target
)
{
--
third
;
}
else
{
++
second
;
}
}
}
return
closest
;
}