zoukankan      html  css  js  c++  java
  • Leetcode0808 有重复字符串的排列组合 去重

     

      JAVA 哈希去重:

    final Set<String> reSet = new HashSet<String>();
    
        public final String[] permutation(String S) {
            search(S, new HashSet<Integer>(), new StringBuilder());
            return setToString(reSet);
        }
    
        private final void search(String s, Set<Integer> path, StringBuilder sb) {
            if (sb.length() == s.length()) {
                String re = sb.toString();
                if (!reSet.contains(re)) {
                    reSet.add(re);
                }
                return;
            }
            for (int i = 0; i < s.length(); i++) {
                if (path.contains(i)) {
                    continue;
                }
                char currentChar = s.charAt(i);
                sb.append(currentChar);
                path.add(i);
                search(s, path, sb);
                path.remove(i);
                sb.deleteCharAt(sb.length() - 1);
            }
        }
    
        private final String[] setToString(Set<String> set) {
            String[] reArr = new String[set.size()];
            Iterator iterator = set.iterator();
            int i = 0;
            while (iterator.hasNext()) {
                reArr[i] = (String) iterator.next();
                i++;
            }
            return reArr;
        }

      JAVA 排序去重:

        List<String> reList = new LinkedList<String>();
    
        public final String[] permutation2(String S) {
            char[] chars = S.toCharArray();
            Arrays.sort(chars);
            search2(chars, new StringBuilder());
            return listToArr(reList);
        }
    
        private final void search2(char[] chars, StringBuilder sb) {
            if (sb.length() == chars.length) {
                reList.add(sb.toString());
                return;
            }
            for (int i = 0; i < chars.length; i++) {
                if (chars[i] == '-' || (i > 0 && chars[i] == chars[i - 1])) {
                    continue;
                }
                char currentChar = chars[i];
                chars[i] = '-';
                search2(chars, sb.append(currentChar));
                chars[i] = currentChar;
                sb.deleteCharAt(sb.length() - 1);
            }
        }
    
        private final String[] listToArr(List<String> list) {
            String[] arr = new String[list.size()];
            for (int i = 0; i < list.size(); i++) {
                arr[i] = list.get(i);
            }
            return arr;
        }

      JS 排序去重:

    /**
     * @param {string} S
     * @return {string[]}
     */
    var permutation = function (S) {
        let sArr = S.split('').sort();
        let reArr = [];
        search(sArr, '', reArr);
        return reArr;
    };
    
    var search = function (str, reStr, reArr) {
        if (str.length == reStr.length) {
            reArr.push(reStr);
            return;
        }
        for (let i = 0; i < str.length; i++) {
            if (str[i] == '-' || (i > 0 && str[i] == str[i - 1])) {
                continue;
            }
            let currentChar = str[i];
            str[i] = '-';
            search(str, reStr + currentChar,reArr);
            str[i] = currentChar;
        }
    }

     

     

  • 相关阅读:
    存储过程中的top+变量
    SQL Server中Table型数据(表变量)与用户自定义函数
    在IE中调用javascript打开Excel
    微软公司软件开发模式简介收集
    一个相当独立的.通用分页控件c#源码一(downmoon收集)
    导出GridView到Excel中的关键之处(downmoon)
    一个相当独立的.通用分页控件c#源码二(downmoon收集)
    三个很常用的存储过程
    前触发器和后触发器简介
    .net2.0中新增的Substitution控件动态更新缓存页的部分(也可用于局部刷新)
  • 原文地址:https://www.cnblogs.com/niuyourou/p/13455106.html
Copyright © 2011-2022 走看看