如果数组中存在一组特定的数字,我该如何计算

8 java algorithm java-8

假设我有一个数字123.我需要看看我是否得到所有数字1到9,包括0.数字123有三个数字:1,2和3.然后我乘以2得到246(我得到数字2,4,6).然后我乘以3得到369.我继续进行增量乘法,直到得到所有数字.

我的方法如下:

public int digitProcessSystem(int N) {
 String number = Integer.toString(N);
 String [] arr = number.split("");
// List <Integer> arr2 = new ArrayList<>();
 for (Integer i = 0; i < arr.length; i++) {
     try {

         arr2[i] = Integer.parseInt(arr[i]);
     } catch (NumberFormatException e) {
         }
     }

 count =0;
 boolean contains = IntStream.of(arr2).anyMatch(x -> x == 1|| x==2 ||x ==  3|| x==4|| x == 5|| x==6 ||x == 7|| x==8||x == 9|| x==0);

}
Run Code Online (Sandbox Code Playgroud)

我真的不知道如何继续为上面第一条路径中不匹配的数字做布尔值,因为我肯定会得到上面布尔搜索中所有数字中的任何一个.如果某些特定数字存在且有些不是这样我可以将实际数字乘以搜索第一次试验中未找到的数字,我该如何得到; 就像我在开始时定义的那样.

Som*_*Guy 3

您可以将其包装到 while 循环中并将数字包含到Set. 一旦集合的大小为 10,所有数字都出现在数字中。I\xc2\xb4d 还建议使用 along而不是 anint,否则你\xc2\xb4将会得到错误的结果或遇到异常。Here\xc2\xb4s 一些示例代码:

\n\n
private static long digitProcessSystem(long N) {\n    long numberN = N;\n    String number = Long.toString(N);\n    // calculate 10 digits number here yet\n    if (number.length() < 10) {\n        // using the smallest possible number with each digit\n        // By using this number we are most likely allmost at the result\n        // This will increase the performance for small digits heavily.\n        long divider = 1023456789L / numberN;\n        numberN *= divider;\n    }\n    number = Long.toString(numberN);\n    String[] arr = number.split("");\n    Set<String> input = new HashSet<>(Arrays.asList(arr));\n    while(input.size() != 10){\n        // add N to number\n        numberN += N;\n        // Parse the new number\n        number = Long.toString(numberN);\n        // split\n        arr = number.split("");\n        // clear set\n        input.clear();\n        // Add the new numbers to the set. If it has the size 10 now the loop will stop and return the number.\n        input.addAll(Arrays.asList(arr));\n    };\n    return numberN;\n}\n\npublic static void main(String[] args) {\n    System.out.println(digitProcessSystem(123));\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

输出:

\n\n
1023458769\n
Run Code Online (Sandbox Code Playgroud)\n