当前位置:网站首页>七月集训(第03天) —— 排序
七月集训(第03天) —— 排序
2022-07-27 09:20:00 【英雄哪里出来】
前言
此为《英雄算法联盟:算法集训》的内容,具体内容详见:知识星球:英雄算法联盟 - 六月集训。加入星球后,即可享用星主 CSDN付费专栏 免费阅读 的权益。
欢迎大家积极在评论区留言发表自己的看法,知无不言,言无不尽,养成每天刷题的习惯,也可以自己发布优质的解题报告,供社区一同鉴赏,吸引一波自己的核心粉丝。
希望大家先自己思考,如果实在没有想法,再看下面的算法思路,如果有思路但是写不出来,可以参考朋友圈中其他人的代码,总有一款是适合你的,关注一下他,取其之长,补给之短。
今天集训的内容是:排序
今天的题可以用系统的 API,也可以自己写排序相关内容。
一、练习题目
| 题目链接 | 难度 |
|---|---|
| 912. 排序数组 | ***** |
| 88. 合并两个有序数组 | ***** |
| 1037. 有效的回旋镖 | ***** |
| 1232. 缀点成线 | ***** |
二、算法思路
1、排序数组
(1)每个语言都会有排序相关的 API,而且基本实现肯定是在 O(nlogn) 级别的,所以直接调用必然不可能超时,直接用就是了。
(2)当然,这道题可以给你用来训练自己手写各种排序用。
class Solution {
public:
vector<int> sortArray(vector<int>& nums) {
sort(nums.begin(), nums.end());
return nums;
}
};
2、合并两个有序数组
(1)凡是力扣上需要排序的题目,都可以直接用 C++ 的 sort;
(2)省时省力!
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
for(int i = 0; i < n; ++i) {
nums1[m+i] = nums2[i];
}
sort(nums1.begin(), nums1.end());
}
};
3、有效的回旋镖
(1)这题其实可以不用排序,但是给大家演示了一下多关键字的排序。
(2)然后就是利用叉乘计算三点共线,两个向量,叉乘结果为 0,代表它们之间的夹角为 0,所以先求出向量 v01 和 v02,然后求叉乘,判断是否为 0 即可。
class Solution {
public:
bool isBoomerang(vector<vector<int>>& points) {
sort(points.begin(), points.end(),
[&]( const vector<int>& a, const vector<int>& b) {
if(a[0] == b[0]) {
return a[1] < b[1];
}
return a[0] < b[0];
});
points[1][0] -= points[0][0];
points[1][1] -= points[0][1];
points[2][0] -= points[0][0];
points[2][1] -= points[0][1];
return points[1][0]*points[2][1] - points[1][1]*points[2][0] != 0;
}
};
4、缀点成线
(1)这题和第三题一样,也是直接采用叉乘计算即可。
class Solution {
public:
bool checkStraightLine(vector<vector<int>>& points) {
sort(points.begin(), points.end(),
[&]( const vector<int>& a, const vector<int>& b) {
if(a[0] == b[0]) {
return a[1] < b[1];
}
return a[0] < b[0];
});
for(int i = 1; i < points.size(); ++i) {
points[i][0] -= points[0][0];
points[i][1] -= points[0][1];
}
for(int i = 2; i < points.size(); ++i) {
if( points[i-1][0]*points[i][1] != points[i][0]*points[i-1][1] ) {
return false;
}
}
return true;
}
};
边栏推荐
- Restful
- [C language - zero foundation _ study _ review _ lesson 4] data types and operations
- 645. Wrong set
- 【CTF】ciscn_2019_es_2
- [C language - zero foundation lesson 11] rotate the pointer of the big turntable
- Linux Installation and remote connection MySQL records
- As a VC, the auction house invested Web3 for the first time
- Mangodb简单使用
- 基于ArkUI eTS开发的坚果笑话(NutJoke)
- Longest string without duplicate characters
猜你喜欢

Sentry 2 orbital data download

Linux Installation and remote connection MySQL records

Analog library function

NCCL 集合通信--Collective Operations

Restful

【ACL2020】一种新颖的成分句法树序列化方法

ES6 new - deconstruction assignment of array / object

As a VC, the auction house invested Web3 for the first time

500 error reporting

HBuilder 微信小程序中运行uni-app项目
随机推荐
Specific methods and steps for Rockwell AB PLC to establish communication with PLC through rslinx classic
[leetcode -- the second day of introduction to programming ability] operator (the number of bit 1 / the difference between the sum of the products of integers)
Nutrecipes developed based on arkui ETS
[C language - zero foundation lesson 6] input and output sentence format and compound sentence
【云原生之kubernetes实战】在kubernetes集群下部署Rainbond平台
ES6 new - string part
Is the operation of assigning values to int variables atomic?
IDL 6S lookup table
1344. Included angle of clock pointer
Mangodb简单使用
【云驻共创】华为云:全栈技术创新,深耕数字化,引领云原生
C language exercise 2
【微信小程序】农历公历互相转换
Function anti chattering throttling
1640. 能否连接形成数组-c语言实现
IDL calls 6S atmospheric correction
1344. 时钟指针的夹角
[C language - zero foundation lesson 7] sequential structure and selection structure
Run uni app project in hbuilder wechat applet
《工程测量学》考试复习总结