class Solution { public: /* * @param A: an integer array * @param target: An integer * @param k: An integer * @return: an integer array */ bool static cmp(int A, int B) { return abs(A) < abs(B) ? true : abs(A) == abs(B) ? A < B : false; } vector<int> kClosestNumbers(vector<int> &A, int target, int k) { // write your code here for (int i = 0; i < A.size(); i++) A[i] -= target; sort(A.begin(), A.end(), cmp); vector<int> res(A.begin(), A.begin()+k); for (int i = 0; i < res.size(); i++) res[i] += target; return res; } };
-------------end of file
thanks for reading-------------