Skip to content

Commit aa503a0

Browse files
authored
Create 1521.Find-a-Value-of-a-Mysterious-Function-Closest-to-Target.cpp
1 parent fc559c6 commit aa503a0

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int closestToTarget(vector<int>& arr, int target) {
4+
unordered_set<int>s;
5+
int ret = INT_MAX;
6+
for (int i=0; i<arr.size(); i++)
7+
{
8+
unordered_set<int>s2;
9+
for (auto x: s)
10+
s2.insert(x&arr[i]);
11+
s2.insert(arr[i]);
12+
for (auto x: s2)
13+
ret = min(ret, abs(x-target));
14+
s = s2;
15+
}
16+
return ret;
17+
}
18+
};

0 commit comments

Comments
 (0)