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. 我关了solution并且删掉个sln.DotSettings.user后似乎也可以了

    "Go To Definition" is disabled in Visual Studio http://social.msdn.microsoft.com/Forums/en ...

  2. Unknown property 'mybatis-plus' yml文件报错

    就是因为没有对应的依赖 package com.taotao.config; import org.mybatis.spring.annotation.MapperScan; import org.s ...

  3. linux系统编程--信号

    信号的概念 man 7 siganl  查看man手册 信号在我们的生活中随处可见, 如:古代战争中摔杯为号:现代战争中的信号弹:体育比赛中使用的信号枪......他们都有共性:1. 简单 2. 不能 ...

  4. CSP-S2019游记——终点

    准退役一年了,回来苟CSP,填补去年留下的遗憾,也算是为这个不那么完美的高中OI生涯划一个句点吧. DAY 1 考前:昨天晚上睡得不太好.早上洛谷打卡居然是中吉(3K说的大吉嘞???).在地铁上有点犯 ...

  5. Spring Cloud Eureka(三):认识Eureka Server 与 Eureka Client

    Spring Cloud Netflix 是什么 This project provides Netflix OSS integrations for Spring Boot apps through ...

  6. Github提交PR(pull request)过程

    PR 想一想, 也可以想执行6.7.8, 再执行4.5. 避免原仓库和fork仓库的冲突 fork到自己的仓库 git clone到本地 git remote add upstream [原项目地址] ...

  7. Mongodb内存管理和使用情况查询

    overview MongoDB使用的是内存映射存储引擎,即Memory Mapped Storage Engine,简称MMAP.MMAP可以把磁盘文件的一部分或全部内容直接映射到内存,这样文件中的 ...

  8. 在编译内核之前到底应该使用make mrproper,make distclean,make clean中的哪个命令呢?

    1. 先找到描述这三个命令的相关信息 在内核目录下使用make help命令可以获取相关信息,信息如下: Cleaning targets: clean - Remove most generated ...

  9. 【8583】ISO8583各域段的说明

    [ISO8583各域段的说明] 1,信息类型(message type)定义位图位置:-格式:定长类型:N4描述:数据包的第一部分,定义数据包的类型.数据类型由数据包的发起者设定,应遵循以下要求:数据 ...

  10. known

    邻接表 https://blog.csdn.net/Violet_ljp/article/details/80556460 Dijkstra 算法实现原理 https://www.jianshu.co ...