LeeCode 587. Erect the Fence

求凸包。
但是这题有个问题就是需要把在边界上的点也求出来,那么在判断cross的时候应该是<0 而不是 <=0。
还有一个问题是,排序的时候当极角相同的时候,按x从小到大的顺序排列的,那么最后几个共线的点顺序应该倒过来,不然结果只包含一个点。

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;




struct Point {
    int x;
    int y;
    Point() : x(0), y(0) {}
    Point(int a, int b) : x(a), y(b) {}
};

Point p0;

bool cmp(Point &a, Point &b)  {
    if ((a.x - p0.x) * ( b.y - p0.y) - ( a.y - p0.y) * ( b.x - p0.x) != 0) {
        return (a.x - p0.x) * (b.y - p0.y) - (a.y - p0.y) * (b.x - p0.x) > 0;
    }
    else if (a.x != b.x){
        return a.x < b.x;
    }
    else {
        return a.y < b.y;
    }
}

int cross(Point a, Point b, Point c) {
    return (a.x - c.x) * (b.y - c.y) - (a.y - c.y) * (b.x - c.x);
}

void display(vector<Point> ans) {
    cout << "---" << endl;
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i].x << " " << ans[i].y << endl;
    }
    cout << "---" << endl;
}


class Solution {
public:
    vector<Point> outerTrees(vector<Point>& points) {
        if (points.size() < 2) {
            return points;
        }
        Point p(1e10, 1e10);
        int min_index = 0;
        for (int i = 0; i < points.size(); i++) {
            if ((points[i].x <= p.x && points[i].y <= p.y) || points[i].x < p.x) {
                min_index = i;
                p = points[i];
            }
        }
        p0 = p;
        swap(points[0], points[min_index]);
        sort(points.begin() + 1, points.end(), cmp);
        int len = points.size();
        if (cross(points[1], points[len - 1], points[0]) != 0) {
            int temp = len - 2;
            while (cross(points[len - 1], points[temp], points[0]) == 0) {
                temp--;
            }
            reverse(points.begin() + temp + 1, points.end());
        }
      //  display(points);
        vector<Point> ans;
        ans.push_back(points[0]);
        ans.push_back(points[1]);
        for (int i = 2; i < points.size(); i++) {
            int t = ans.size();
            while (ans.size() > 1 && cross(ans[t - 1], points[i], ans[t - 2]) < 0) {
                ans.pop_back();
                t = ans.size();
            }
            ans.push_back(points[i]);
        }
        return ans;
    }
};

int main() {
    Solution a;
    vector<Point> points = {{3, 0}, {4, 0}, {5, 0}, {6, 1}, {7, 2}, {7, 3}, {7, 4}, {6, 5}, {5, 5}, {4, 5}, {3, 5}, {2, 5}, {1, 4}, {1, 3}, {1, 2}, {2, 1}};
    vector<Point> ans = a.outerTrees(points);
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i].x << " " << ans[i].y << endl;
    }
    return 0;
}
/*
{{1, 1}, {2, 2}, {2, 0}, {2, 4}, {3, 3}, {4, 2}}
{{3, 0}, {4, 0}, {5, 0}, {6, 1}, {7, 2}, {7, 3}, {7, 4}, {6, 5}, {5, 5}, {4, 5}, {3, 5}, {2, 5}, {1, 4}, {1, 3}, {1, 2}, {2, 1}, {4, 2}, {0, 3}}
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段代码是一个 Vue.js 的单文件组件,包含了一个模板 `<template>` 和组件的结构。 在模板中,组件的内容被包含在一个 `div` 标签中,而每个组件都被使用自定义标签的形式引入,例如 `<header-part />`、`<search />`、`<category />` 等等。这些自定义标签的实现在 Vue.js 的 JavaScript 代码中定义。 具体来说: - `<header-part />` 组件用于渲染页面的头部,包括网站的 logo、导航栏等等。 - `<search />` 组件用于渲染页面中的搜索栏。 - `<category />` 组件用于渲染商品分类列表。 - `<rotation />` 组件用于渲染轮播图,展示一些特别推荐的商品信息。 - `<erect-rotation />` 组件用于渲染竖向的轮播图,也是用于展示推荐商品信息的。 - `<serve />` 组件用于渲染页面中的服务列表。 - `<sec-kill />` 组件用于渲染秒杀商品列表。 - `<recommend />` 组件用于渲染推荐商品列表。 - `<my-footer />` 组件用于渲染页面的底部信息。 组件的实现代码应该在 JavaScript 中,通过 Vue.js 的组件定义方式来定义,例如: ```javascript Vue.component('header-part', { // 组件的选项 }) ``` 这样的组件定义方式需要在 Vue.js 的入口 JavaScript 文件中进行定义,然后才能在模板中使用。 总体来说,这段代码是一个典型的 Vue.js 单文件组件,用于构建京东商城页面的组件化。通过将页面拆分为多个组件,可以更加方便地管理和维护页面的代码,并且可以实现代码的重用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值