Given an array with distinct numbers, return an integer indicating the minimum number of swap operations required to sort the array into ascending order.

Example 1:

Input: [5, 1, 3, 2]
Output: 2
Explanation: [5, 1, 3, 2] -> [2, 1, 3, 5] -> [1, 2, 3, 5]

Example 2:

Input: [1, 3, 2]
Output: 1
Explanation: [1, 3, 2] -> [1, 2, 3]
分析:
先把原数组保存一份,然后对另一份排序,并且把原数组的值与index保存一份。这样,我们需要得到某个数的位置的时候,直接通过map得到。
 public class Solution {
public int minSwaps(int[] elems) {
int counter = ;
Map<Integer, Integer> map = buildMap(elems);
int[] copy = new int[elems.length];
System.arraycopy(elems, , copy, , elems.length);
Arrays.sort(elems); for (int i = ; i < copy.length; i++) {
if (copy[i] != elems[i]) {
swap(copy, i, map.get(elems[i]), map);
counter++;
}
}
return counter;
} private Map<Integer, Integer> buildMap(int[] elems) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = ; i < elems.length; i++) {
map.put(elems[i], i);
}
return map;
} private void swap(int[] elems, int i, int j, Map<Integer, Integer> map) {
map.put(elems[j], i);
map.put(elems[i], j);
int temp = elems[j];
elems[j] = elems[i];
elems[i] = temp;
}
}
 
 

Min swaps to sort array的更多相关文章

  1. js & sort array object

    js & sort array object sort array object in js https://flaviocopes.com/how-to-sort-array-of-obje ...

  2. 42.旋转数组的最小元素[Get min value of rotated array]

    [题目] 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个排好序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3, 4, 5, 1, 2}为{1, 2, 3, 4, 5 ...

  3. LeetCode 922. Sort Array By Parity II C++ 解题报告

    922. Sort Array By Parity II 题目描述 Given an array A of non-negative integers, half of the integers in ...

  4. LeetCode 905. Sort Array By Parity

    905. Sort Array By Parity Given an array A of non-negative integers, return an array consisting of a ...

  5. Sort Array By Parity II LT922

    Given an array A of non-negative integers, half of the integers in A are odd, and half of the intege ...

  6. [LeetCode] 922. Sort Array By Parity II 按奇偶排序数组之二

    Given an array A of non-negative integers, half of the integers in A are odd, and half of the intege ...

  7. 【LEETCODE】42、922. Sort Array By Parity II

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  8. 【LEETCODE】41、905. Sort Array By Parity

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  9. Relative Sort Array

    Relative Sort Array Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all eleme ...

随机推荐

  1. SQL SERVER 表添加新字段

    SQL SERVER 表添加新字段 ALTER TABLE doc_exa ADD column_b VARCHAR(20) NULL; -- doc_exa 是表名 -- column_b 是新加的 ...

  2. 微信小程序填坑之旅(1)-app.js中用云开发获取openid,在其他页上用app.globaldata.openid获取为空

    参考:小程序如何在其他页面监听globalData中值的变化?https://www.jianshu.com/p/8d1c4626f9a3 原因就是:app.js没执行完时,其他页已经onload了, ...

  3. 两种dp模型

    两个常见模型 bzoj 4321 题意:编号为1~n的人排成一排,问有多少种排法使得任意相邻两人的编号之差不为1或-1. n<=1000 排列计数问题:考虑把数从小到大插入的过程进行dp. 设 ...

  4. 数据结构实验之链表四:有序链表的归并(SDUT 2119)

    #include <bits/stdc++.h> using namespace std; struct node { int data; struct node *next; }; st ...

  5. leetcode题目234.回文链表(快慢指针+辅助空间-简单)

    题目描述: 请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 进阶: 你能否用 O( ...

  6. Linux编程之recvmsg和sendmsg函数

    recvmsg 和 sendmsg 函数 #include <sys/types.h> #include <sys/socket.h> ssize_t send(int soc ...

  7. linux系统空间不足,不重启进程,清理僵尸文件。

    问题:通过lsof |grep delete命令可以看到状态为delete的进程文件占用了较多的空间,导致系统磁盘空间不足,而du 命令看到的磁盘空间占用没那么高. 得到僵尸文件名称:catalina ...

  8. centos7 下设置 mongodb 开机启动 (重点)

    centos 7的开机启动跟之前版本的centos有很大不同.现在用 systemctl命令代替了之前的chkconfig 和 service 命令 注册到开机启动的方法如下: 在系统服务目录下新建m ...

  9. mysql数据库学习

    1,服务端和客户端 MySQL 包括服务端和客户端,服务端是MySQL server,客户端包括命令行客户端和图形用户客户端: 命令行客户端:mysql,mysqladmin,mysqldump  ( ...

  10. 用第三方工具类,将JavaBean、List、Map<String,Object>转成JSON文本

    导入第三方jar包: >commons-beanutils-1.7.0.jar >commons-collections-3.1.jar >commons-lang-2.5.jar ...