注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
- #include <iostream>
- #include <vector>
- #include <map>
- #include <unordered_map>
- using namespace std;
- class Solution {
- public:
- pair<int, int> p1(vector<int>& nums) {
- if (nums.empty()) {
- return make_pair(0, 0);
- }
- int sum = 0, m = 0;
- for (int i = 0, n = nums.size(); i < n; i++) {
- if (i % 2 == 0) {
- sum += nums.at(i);
- m++;
- }
- }
- return make_pair(sum, floor((double)sum / (double)m));
- }
- vector<int> p2(vector<int>& nums, int k) {
- if (nums.empty() or k <= 0) {
- return {};
- }
- map<int, int> h;
- for (const auto &i : nums) {
- h[i]++;
- }
- vector<int> t;
- for (const auto &i : h) {
- if (i.second == k) {
- t.push_back(i.first);
- }
- }
- for (const auto &i : t) {
- h.erase(i);
- }
- vector<int> result;
- for (const auto &i : h) {
- for (int j = 0; j < i.second; j++) {
- result.push_back(i.first);
- }
- }
- return result;
- }
- bool p3(pair<int, int> A, pair<int, int> B, pair<int, int> P, pair<int, int> Q) {
- pair<int, int> a, b, c, d;
- a = make_pair(P.first - A.first, P.second - A.second);
- b = make_pair(Q.first - A.first, Q.second - A.second);
- c = make_pair(P.first - B.first, P.second - B.second);
- d = make_pair(Q.first - B.first, Q.second - B.second);
- int x = a.first * b.second - a.second * b.first, y = c.first * d.second - c.second * d.first;
- return (x < 0 and y < 0) or (x > 0 and y > 0);
- }
- int p4(vector<int>& nums) {
- unordered_map<int, int> h;
- for (const auto &i : nums) {
- h[i]++;
- }
- map<int, vector<int>> treemap;
- for (const auto &i : h) {
- treemap[i.second].push_back(i.first);
- }
- return prev(prev(end(treemap)))->second.front();
- }
- int p5(vector<int>& nums, int k) {
- int a = INT_MAX, b = INT_MAX;
- for (const auto &i : nums) {
- if (a == INT_MAX) {
- a = i;
- continue;
- }
- if (b == INT_MAX) {
- int d1 = abs(a - k), d2 = abs(i - k);
- if (d1 <= d2) {
- b = i;
- continue;
- }
- b = a;
- a = i;
- continue;
- }
- int d1 = abs(a - k), d2 = abs(b - k), d3 = abs(i - k);
- if (d3 <= d1) {
- b = a;
- a = i;
- continue;
- }
- if (d3 <= d2) {
- b = i;
- continue;
- }
- }
- return a + b;
- }
- };
- int main(void) {
- Solution solution;
- vector<int> nums1 = {1, -2, 2, 3, -3, 4, -5};
- pair<int, int> result1 = solution.p1(nums1);
- cout << result1.first << ',' << result1.second << '\t';
- nums1 = {1, 2, 3};
- result1 = solution.p1(nums1);
- cout << result1.first << ',' << result1.second << '\n';
- vector<int> nums2 = {4, 4, 4, 3, 2, 1};
- int n2 = 3;
- vector<int> result2 = solution.p2(nums2, n2);
- for (const auto &i : result2) {
- cout << i << '\t';
- }
- cout << '\n';
- pair<int, int> A, B, P, Q;
- A = make_pair(1, -1);
- B = make_pair(1, 1);
- P = make_pair(0, 0);
- Q = make_pair(2, 0);
- cout << boolalpha << solution.p3(A, B, P, Q) << '\t';
- A = make_pair(1, 1);
- B = make_pair(1, -1);
- P = make_pair(2, 0);
- Q = make_pair(0, 0);
- cout << boolalpha << solution.p3(A, B, P, Q) << '\t';
- A = make_pair(1, -1);
- B = make_pair(1, 1);
- P = make_pair(2, 0);
- Q = make_pair(2, 2);
- cout << boolalpha << solution.p3(A, B, P, Q) << '\t';
- A = make_pair(1, -1);
- B = make_pair(1, 1);
- P = make_pair(1, 0);
- Q = make_pair(1, 2);
- cout << boolalpha << solution.p3(A, B, P, Q) << '\t';
- A = make_pair(-1, 1);
- B = make_pair(1, 1);
- P = make_pair(-2, 1);
- Q = make_pair(1, 1);
- cout << boolalpha << solution.p3(A, B, P, Q) << '\t';
- A = make_pair(-1, 0);
- B = make_pair(1, 0);
- P = make_pair(1, 0);
- Q = make_pair(1, 1);
- cout << boolalpha << solution.p3(A, B, P, Q) << '\t';
- A = make_pair(-1, 0);
- B = make_pair(1, 0);
- P = make_pair(-1, 1);
- Q = make_pair(1, 1);
- cout << boolalpha << solution.p3(A, B, P, Q) << '\t';
- cout << '\n';
- vector<int> nums4 = {1, -2, 2, -3, -3, 4, -5, -2, -4};
- cout << solution.p4(nums4) << '\t';
- nums4 = {0, 1, 2, 2, 2, 2, 1, 0, 5, 1};
- cout << solution.p4(nums4) << '\t';
- vector<int> nums5 = {1, -2, 2, -3, -3, 4, -5, -2, -4};
- int n4 = -1;
- cout << solution.p5(nums5, n4) << '\t';
- nums5 = {-2, -2, -1, -1, 1, 1, 2, 3};
- n4 = 0;
- cout << solution.p5(nums5, n4) << '\t';
- nums5 = {-15, -14, -9, -28, -17, 0, 6, 7, -6, -29};
- n4 = 5;
- nums5 = {21, 6, 27, 18};
- n4 = 15;
- cout << solution.p5(nums5, n4) << '\t';
- cout << solution.p5(nums5, n4) << '\n';
- cout << "\nPassed All\n";
- return 0;
- }
复制代码
补充内容 (2017-4-19 06:46:43):
过掉全部test |