N 对情侣坐在连续排列的 2N 个座位上,想要牵到对方的手。 计算最少交换座位的次数,以便每对情侣可以并肩坐在一起。 次交换可选择任意两人,让他们站起来交换座位。

人和座位用 0 到 2N-1 的整数表示,情侣们按顺序编号,第一对是 (0, 1),第二对是 (2, 3),以此类推,最后一对是 (2N-2, 2N-1)

这些情侣的初始座位  row[i] 是由最初始坐在第 i 个座位上的人决定的。

示例 1:

  1. 输入: row = [0, 2, 1, 3]
  2. 输出: 1
  3. 解释: 我们只需要交换row[1]和row[2]的位置即可。

示例 2:

  1. 输入: row = [3, 2, 0, 1]
  2. 输出: 0
  3. 解释: 无需交换座位,所有的情侣都已经可以手牵手了。
  4.  
  1. class Solution {
  2. public int minSwapsCouples(int[] row) {
  3. int n=0;
  4. //只需要遍历下标为偶数的座位,每次+2,奇数的座位可由交换座位匹配好
  5. for(int i=0;i<=row.length-2;i+=2){
  6. int j;
  7. //判断下标i的情侣是多少
  8. j=row[i]%2==0?row[i]+1:row[i]-1;
  9. //若下一个座位的不是该情侣
  10. if(row[i+1]!=j){
  11. //需要进行交换座位
  12. n++;
  13. //找到该情侣,进行交换
  14. for(int m=i+2;m<row.length;m++){
  15. if(row[m]==j){
  16. swap(row,m,i+1);
  17. break;
  18. }
  19. }
  20. }
  21. }
  22. return n;
  23. }
  24.  
  25. //交换
  26. private void swap(int[] a,int i,int j){
  27. int tmp=a[i];
  28. a[i]=a[j];
  29. a[j]=tmp;
  30. }
  31. }

LeetCode-765.情侣牵手的更多相关文章

  1. Java实现 LeetCode 765 情侣牵手(并查集 || 暴力)

    765. 情侣牵手 N 对情侣坐在连续排列的 2N 个座位上,想要牵到对方的手. 计算最少交换座位的次数,以便每对情侣可以并肩坐在一起. 一次交换可选择任意两人,让他们站起来交换座位. 人和座位用 0 ...

  2. Leetcode之并查集专题-765. 情侣牵手(Couples Holding Hands)

    Leetcode之并查集专题-765. 情侣牵手(Couples Holding Hands) N 对情侣坐在连续排列的 2N 个座位上,想要牵到对方的手. 计算最少交换座位的次数,以便每对情侣可以并 ...

  3. [LeetCode] 765. Couples Holding Hands 情侣牵手

    N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum numb ...

  4. [Swift]LeetCode765. 情侣牵手 | Couples Holding Hands

    N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum numb ...

  5. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  6. C#LeetCode刷题-贪心算法

    贪心算法篇 # 题名 刷题 通过率 难度 44 通配符匹配   17.8% 困难 45 跳跃游戏 II   25.5% 困难 55 跳跃游戏   30.6% 中等 122 买卖股票的最佳时机 II C ...

  7. 2019年9月Leetcode每日训练日志

    2019-09-16 #1171 从链表中删去总和值为零的连续节点 #1170 比较字符串最小字母出现频次 #1169 查询无效交易 #226 翻转二叉树 2019-09-15 #1190 反转每对括 ...

  8. leetcode难题

    4 寻找两个有序数组的中位数       35.9% 困难     10 正则表达式匹配       24.6% 困难     23 合并K个排序链表       47.4% 困难     25 K ...

  9. C#LeetCode刷题-图

    图篇 # 题名 刷题 通过率 难度 133 克隆图   18.7% 中等 207 课程表   40.0% 中等 210 课程表 II   40.0% 中等 310 最小高度树   29.5% 中等 3 ...

随机推荐

  1. 64位Windows的Dos中取消了edit命令

    前段时间在玩dos命令行的时候,用copy con创建了txt文件后想对其进行编辑,然后我又不想用记事本,所以去网上找命令行中对文本文件进行编辑的命令(纯属想装B),结果看到了edit命令. 一敲,就 ...

  2. FIO_工具_专业

    一.FIO工具安装: 1.查看fio是否安装 [root@localhost /]#rpm –qa|grep fio 2.源码安装(推荐) 官网地址:http://freecode.com/proje ...

  3. k8s 隔离context+namespace

    kubernetes 最简单的隔离是 应用间使用 namespace进行,对应不同项目,namespace 不同,那么相互调用使用 dns.namespace,而使用 context + namesp ...

  4. ORA-27125: unable to create shared memory segment的解决方法(转)

    ORA-27125: unable to create shared memory segment的解决方法(转) # Kernel sysctl configuration file for Red ...

  5. Android Environment 获取各种路径的方法

    <pre name="code" class="java">package com.deepoon.beyond.environment; impo ...

  6. leetcode88—Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...

  7. 树莓派学习笔记(3):利用VNC远程控制树莓派

    转载请注明:@小五义http://www.cnblogs.com/xiaowuyi      等了一个十一假期,新买的B+终于到了.按照前两节的方法,重新安装了操作系统. 一.添加国内软件源 Rasp ...

  8. memcpy、memmove、memset、memchr、memcmp、strstr详解

    第一部分 综述 memcpy.memmove.memset.memchr.memcmp都是C语言中的库函数,在头文件string.h中.memcpy和memmove的作用是拷贝一定长度的内存的内容,m ...

  9. 让win7变成无线路由(需要用管理员权限打开)最后完善.rar

    让win7变成无线路由(需要用管理员权限打开)最后完善.bat @ECHO OFF CLS color 0a netsh wlan show drivers ECHO.★★★★★★★★★★★★★★★★ ...

  10. 【服务器】Https服务配置

    1)利用openssl生成证书 2)再次修改nginx配置文件nginx.conf中的server配置 ① 是默认监听http请求的8080端口的 server    (再次修改,第一次是在 用ngi ...