Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何对所有可能的字符串组合进行编码?_Java_Combinations - Fatal编程技术网

Java 如何对所有可能的字符串组合进行编码?

Java 如何对所有可能的字符串组合进行编码?,java,combinations,Java,Combinations,假设我有字符串word=“hello12” 我需要所有可能的特殊字符组合,而不是数字(使用shift+number时得到的字符)。所以,我想得到的结果是hello12,你好!你好,你好 我所做的是用所有的情况(1='!',2='@'…)创建开关,但我不知道如何对所有的组合进行编码。我所能编码的就是用特殊符号更改所有数字(代码如下) char[]passwordInCharArray; 对于(int i=0;i基于代码from:,我提出了这个实现,它可以满足您的需要。它将找到字符串中所有数字的索引

假设我有
字符串word=“hello12”

我需要所有可能的特殊字符组合,而不是数字(使用shift+number时得到的字符)。所以,我想得到的结果是hello12,你好!你好,你好

我所做的是用所有的情况(1='!',2='@'…)创建开关,但我不知道如何对所有的组合进行编码。我所能编码的就是用特殊符号更改所有数字(代码如下)

char[]passwordInCharArray;

对于(int i=0;i基于代码from:,我提出了这个实现,它可以满足您的需要。它将找到字符串中所有数字的索引,然后生成所有可能的数字,在这些数字中可以用特殊字符替换

import java.util.*;

public class Comb {

    public static List<String> combinations(String pass) {
        String replace = ")!@#$%^&*(";
        char[] password = pass.toCharArray();
        List<Integer> index = new ArrayList<Integer>();
        List<String> results = new ArrayList<String>();
        results.add(pass);

        //find all digits
        for (int i = 0; i < password.length; i++) {
            if (Character.isDigit(password[i])) {
                index.add(i);
            }
        }

        //generate combinations
        int N = (int) Math.pow(2d, Double.valueOf(index.size()));  
        for (int i = 1; i < N; i++) {
            String code = Integer.toBinaryString(N | i).substring(1);
            char[] p = Arrays.copyOf(password, password.length);

            //replace the digits with special chars
            for (int j = 0; j < index.size(); j++) {
                if (code.charAt(j) == '1') {
                    p[index.get(j)] = replace.charAt(p[index.get(j)] - '0');
                }
            }
            results.add(String.valueOf(p));
        }
        return results;
    }

    public static void main(String... args) {
        System.out.println(combinations("hello12"));
    }
}
import java.util.*;
公共类梳子{
公共静态列表组合(字符串传递){
字符串替换=“)!@$%^&*(”;
char[]password=pass.toCharArray();
列表索引=新的ArrayList();
列表结果=新建ArrayList();
结果:添加(通过);
//查找所有数字
for(int i=0;i
在代码from:的基础上,我提出了这个实现,它可以满足您的需要。它将找到字符串中所有数字的索引,然后生成所有可能的数字,在这些数字中可以用特殊字符替换

import java.util.*;

public class Comb {

    public static List<String> combinations(String pass) {
        String replace = ")!@#$%^&*(";
        char[] password = pass.toCharArray();
        List<Integer> index = new ArrayList<Integer>();
        List<String> results = new ArrayList<String>();
        results.add(pass);

        //find all digits
        for (int i = 0; i < password.length; i++) {
            if (Character.isDigit(password[i])) {
                index.add(i);
            }
        }

        //generate combinations
        int N = (int) Math.pow(2d, Double.valueOf(index.size()));  
        for (int i = 1; i < N; i++) {
            String code = Integer.toBinaryString(N | i).substring(1);
            char[] p = Arrays.copyOf(password, password.length);

            //replace the digits with special chars
            for (int j = 0; j < index.size(); j++) {
                if (code.charAt(j) == '1') {
                    p[index.get(j)] = replace.charAt(p[index.get(j)] - '0');
                }
            }
            results.add(String.valueOf(p));
        }
        return results;
    }

    public static void main(String... args) {
        System.out.println(combinations("hello12"));
    }
}
import java.util.*;
公共类梳子{
公共静态列表组合(字符串传递){
字符串替换=“)!@$%^&*(”;
char[]password=pass.toCharArray();
列表索引=新的ArrayList();
列表结果=新建ArrayList();
结果:添加(通过);
//查找所有数字
for(int i=0;i
理论 组合通常更容易用(调用自身的方法)表示

我认为通过一个例子,该算法更容易理解,因此让我们以
stringword=hello12
为例

我们将迭代每个字符,直到找到一个数字。第一个是
1
。此时,我们可以想象单词被虚拟光标一分为二:

  • hello
    位于左侧。我们知道它不会改变
  • 12
    位于右侧。每个字符都可能是一个数字,因此会发生变化
要检索所有可能的组合,我们希望:

  • 保留单词的第一部分
  • 计算单词第二部分的所有可能组合
  • 将这些组合中的每一个附加到单词的第一部分
  • 下面的树表示我们要计算的内容(根是单词的第一部分,每个分支表示一个组合)

    您需要编写一个算法来收集此树的所有分支

    Java代码 /!\n在查看代码之前,我建议您尝试实现我上面描述的内容:这就是我们改进自己的方式!

    以下是相应的Java代码:

    publicstaticvoidmain(字符串[]args){
    集合组合=组合(“hello12”);
    forEach(System.out::println);
    }
    公共静态集合组合(字符串字){
    //将包含所有单词的组合
    集合组合=新的HashSet();
    //这个词是一个组合词(如果为空,则可以忽略)
    添加(单词);
    //迭代每个单词的字符
    for(int i=0;i=word.length();
    字符串secondWordPart=isWordEnd?“:word.substring(i+1);
    //诀窍是:我们计算第二个单词部分的所有组合。。。
    设置allCombinationsOfSecondPart=combinate(secondWordPart);
    //…我们把它们一个接一个地加到第一个单词的后面
    for(字符串:第二部分的所有组合){
    字符串组合=firstWordPart+get
    
    hello
    ├───1
    │   ├───2 (-> hello12)
    │   └───@ (-> hello1@)
    └───!
        ├───2 (-> hello!2)
        └───@ (-> hello!@)