今日のシェアフォース控除== 1202。文字列==および検索セット内の要素を交換します(Java8マップcomputeIfAbsentメソッドの説明)

出典:リンク:https://leetcode-cn.com/problems/smallest-string-with-swaps/
免責事項:誰かの権利を侵害した場合は、私に連絡してください。削除します。
専門家を歓迎してスプレーしてください。

タイトル説明

文字列sと、文字列内のいくつかの「インデックスペア」配列ペアを指定します。ここで、pairs [i] = [a、b]は、文字列内の2つのインデックスを表します(番号付けは0から始まります)。
インデックスの任意のペアの文字を、ペアで何度でも交換できます。
sが数回の交換後になり得る最小の文字列を辞書式に返します。

入力:s = "dcab"、ペア= [[0,3]、[1,2]]
出力: "bacd"
説明:
交換s [0]およびs [3]、s = "bcad"
交換s [1 ]そしてs [2]、s = "bacd"

入力:s = "dcab"、ペア= [[0,3]、[1,2]、[0,2]]
出力: "abcd"
説明:
s [0]とs [3]を交換します。s = " 「bcad」は
s [0]とs [2]を
交換し、s = "acbd"はs [1]とs [2]を交換します。s = "abcd"

入力:s = "cba"、ペア= [[0,1]、[1,2]]
出力: "abc"
説明:
s [0]とs [1]を
交換します。s= "bca"はs [1を交換します。 ]およびs [2]、s = "bac"
交換s [0]およびs [1]、s = "abc"

回答

詳細なコードのアイデアについては、こちらをご覧ください:https//leetcode-cn.com/problems/smallest-string-with-swaps/solution/1202-jiao-huan-zi-fu-chuan-zhong-de-yuan-wgab //

  1. ASCII値が低いほど、文字列内の位置が高くなり、文字列全体の辞書式順序が高くなります。
  2. 同じ連結成分に属するすべての文字を見つける必要があります。「接続された」インデックスを文字ASCII値の昇順でソートします
  3. ユニオン検索セット
    ステップ1:最初にペアでインデックスペアをトラバースし、インデックスペアにペアのインデックスを入力してセットを検索すると、検索セットは同じ連結成分の要素をマージするのに役立ちます。注:「インデックス」は、複合検索によって管理される「文字」ではありません。
    ステップ2:各インデックスの入力文字列sをトラバースし、結合された検索セットでインデックスの代表を見つけ、同じ代表に属する文字をまとめます。このステップでは、マッピング関係を確立する必要があります。キー:セット内の代表的な要素を確認します。値:同じ代表的な要素に属するs内の文字。ハッシュテーブルを使用して、マッピング関係を確立できます。
    ステップ3:グループ化して並べ替えます。これは、同じ接続コンポーネント内の文字を並べ替えることです。

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;

public class Solution {
    
    

    public String smallestStringWithSwaps(String s, List<List<Integer>> pairs) {
    
    
        if (pairs.size() == 0) {
    
    
            return s;
        }

        // 第 1 步:将任意交换的结点对输入并查集
        int len = s.length();
        UnionFind unionFind = new UnionFind(len);
        for (List<Integer> pair : pairs) {
    
    
            int index1 = pair.get(0);
            int index2 = pair.get(1);
            unionFind.union(index1, index2);
        }

        // 第 2 步:构建映射关系
        char[] charArray = s.toCharArray();
        // key:连通分量的代表元,value:同一个连通分量的字符集合(保存在一个优先队列中)
        Map<Integer, PriorityQueue<Character>> hashMap = new HashMap<>(len);
        for (int i = 0; i < len; i++) {
    
    
            int root = unionFind.find(i);
//            if (hashMap.containsKey(root)) {
    
    
//                hashMap.get(root).offer(charArray[i]);
//            } else {
    
    
//                PriorityQueue<Character> minHeap = new PriorityQueue<>();
//                minHeap.offer(charArray[i]);
//                hashMap.put(root, minHeap);
//            }
            // 上面六行代码等价于下面一行代码,JDK 1.8 以及以后支持下面的写法
            hashMap.computeIfAbsent(root, key -> new PriorityQueue<>()).offer(charArray[i]);
        }

        // 第 3 步:重组字符串
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < len; i++) {
    
    
            int root = unionFind.find(i);
            stringBuilder.append(hashMap.get(root).poll());
        }
        return stringBuilder.toString();
    }

    private class UnionFind {
    
    

        private int[] parent;
        /**
         * 以 i 为根结点的子树的高度(引入了路径压缩以后该定义并不准确)
         */
        private int[] rank;

        public UnionFind(int n) {
    
    
            this.parent = new int[n];
            this.rank = new int[n];
            for (int i = 0; i < n; i++) {
    
    
                this.parent[i] = i;
                this.rank[i] = 1;
            }
        }

        public void union(int x, int y) {
    
    
            int rootX = find(x);
            int rootY = find(y);
            if (rootX == rootY) {
    
    
                return;
            }

            if (rank[rootX] == rank[rootY]) {
    
    
                parent[rootX] = rootY;
                // 此时以 rootY 为根结点的树的高度仅加了 1
                rank[rootY]++;
            } else if (rank[rootX] < rank[rootY]) {
    
    
                parent[rootX] = rootY;
                // 此时以 rootY 为根结点的树的高度不变
            } else {
    
    
                // 同理,此时以 rootX 为根结点的树的高度不变
                parent[rootY] = rootX;
            }
        }

        public int find(int x) {
    
    
            if (x != parent[x]) {
    
    
                parent[x] = find(parent[x]);
            }
            return parent[x];
        }
    }
} 

Java8マップcomputeIfAbsentメソッドの説明

computeIfAbsent

// 方法定义
default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
    
    
    ...
}

// java8之前。从map中根据key获取value操作可能会有下面的操作
Object key = map.get("key");
if (key == null) {
    
    
    key = new Object();
    map.put("key", key);
}

// java8之后。上面的操作可以简化为一行,若key对应的value为空,会将第二个参数的返回值存入并返回
Object key2 = map.computeIfAbsent("key", k -> new Object());

おすすめ

転載: blog.csdn.net/qq_45531729/article/details/112913136