BiliBili漫画下载神器:如何快速搭建个人离线漫画库
2026/6/11 7:47:11
Problem: 765. Couples Holding Hands 情侣牵手
https://leetcode.com/problems/couples-holding-hands/description/comments/1923078/
贪心,每次遇到不匹配的,拿后面匹配的交换即可,最后统计次数,就可以,官方题解的并查集
class Solution { public: int minSwapsCouples(vector<int>& row) { int num = 0; for(int i = 0; i < row.size(); i += 2) { if(row[i]%2==0 && row[i+1]!=row[i]+1) { for(int j = i+2; j < row.size(); j++) { if(row[j]==row[i]+1) { swap(row[j], row[i+1]); num++; break; } } } else if(row[i]%2==1 && row[i+1]!=row[i]-1) { for(int j = i+2; j < row.size(); j++) { if(row[j]==row[i]-1) { swap(row[j], row[i+1]); num++; break; } } } } return num; } };