简体   繁体   English

整数数组,如果有 2 个连续的数字是 7 或有两个 7 被数字分隔,则返回 true。 仅使用 Java 流

[英]array of integers,return true if there are 2 consecutive numbers which are 7 or there are two 7s separated by a number . Using Java Streams only

Given an array of integers, return true if the array contains two 7's next to each other, or there are two 7's separated by one element. **Using Java Streams only**
Example1: [1, 7, 7] → true
Example2: [1, 7, 1, 7] → true
Example3: [1, 7, 1, 1, 7] → false
Example4: [7, 7, 1, 1, 7] → true
Example5: [9, 0, 5, 1, 7] → false
Example6: [7, 7, 7, 7, 7] → true

Please help, i am able to solve this using regular for loop, but i need the solution in java Streams请帮忙,我可以使用常规 for 循环来解决这个问题,但我需要 Java Streams 中的解决方案

public void static void main(String[] args) {
        Integer a[] = { 1, 2, 1, 7, 3, 5, 2, 7 };
        boolean flag = false;
        for (int i = 0; i < a.length - 1; i++) {
            if (a[i] == 7 && a[i + 1] == 7 || a[i] == 7 && a[i + 1] == 1 && a[i + 2] == 7) {
                flag = true;
            }
        }
        System.out.println(flag);
    }
 

//is it possible to avoid the for loop, and solve this using java streams – //是否可以避免for循环,并使用java流解决这个问题 –

Here's a very basic answer.这是一个非常基本的答案。 It might not be the smartest solution but i can't think of anything better on the spot.这可能不是最聪明的解决方案,但我无法立即想到更好的解决方案。

IntStream.range(0, a.length-1)
         .anyMatch( i -> a[i] == 7 && a[i+1] ==7 || a[i] == 7 
                    && a[i + 1] == 1 && a[i + 2] == 7 );

And please next time provide your code in the question itself, to make helping you easier.下次请在问题本身中提供您的代码,以便更轻松地帮助您。

To do it with streams, you can write a custom Collector , and use it like this:要使用流来做到这一点,您可以编写一个自定义Collector ,并像这样使用它:

// With an int[]
boolean flag = Arrays.stream(arr).boxed().collect(TwoSevens.collector());

// With an Integer[]
boolean flag = Arrays.stream(arr).collect(TwoSevens.collector());

// With a List<Integer>
boolean flag = list.stream().collect(TwoSevens.collector());
class TwoSevens {
    public static Collector<Integer, ?, Boolean> collector() {
        return Collector.of(TwoSevens::new, TwoSevens::add,
                (a,b) -> { throw new UnsupportedOperationException("Parallel processing not supported"); },
                TwoSevens::getResult);
    }
    private int prev1, prev2;
    private boolean result;
    private TwoSevens() {/*nothing to do*/}
    private void add(int value) {
        if (value == 7 && (prev1 == 7 || prev2 == 7))
            this.result = true;
        prev1 = prev2;
        prev2 = value;
    }
    private boolean getResult() {
        return this.result;
    }
}

Test测试

int[][] tests = { { 1, 7, 7 },
                  { 1, 7, 1, 7 },
                  { 1, 7, 1, 1, 7 },
                  { 7, 7, 1, 1, 7 },
                  { 9, 0, 5, 1, 7 },
                  { 7, 7, 7, 7, 7 } };
for (int[] arr : tests) {
    boolean flag = Arrays.stream(arr).boxed().collect(TwoSevens.collector());
    System.out.println(Arrays.toString(arr) + ": " + flag);
}

Output输出

[1, 7, 7]: true
[1, 7, 1, 7]: true
[1, 7, 1, 1, 7]: false
[7, 7, 1, 1, 7]: true
[9, 0, 5, 1, 7]: false
[7, 7, 7, 7, 7]: true

Probably an unorthodox way to approach this task but how about converting the numbers to string and using regex?可能是处理此任务的非正统方法,但是如何将数字转换为字符串并使用正则表达式?

public void static void main(String[] args) {
    Integer a[] = { 1, 2, 1, 7, 3, 5, 2, 7 };
    boolean flag = Arrays.stream(a)
                         .map(String::valueOf)
                         .collect(Collectors.joining())
                         .matches("\\d*77\\d*|\\d*7\\d7\\d*");
    System.out.println(flag);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建一个从 1 到 10 的随机数组。然后,在 java 中将所有 7 移动到数组的前面 - Create a random array of numbers from 1 – 10. Then, move all of the 7s to the front of the array in java 使用Java流检查两个日期是否连续 - Check if two dates are consecutive using java streams Ubuntu-为什么我有那么多Oracle Java 7,应该使用哪一个? - Ubuntu - Why do I have so many Oracle Java 7s, and which one should I be using? 返回仅包含奇数整数的数组 - return an Array which contains only odd integers 使用 Java 流仅基于属性对连续元素进行分组 - Grouping only consecutive elements based on property using Java Streams 取一个 integer 数组并仅打印 java 中连续 3 顺序的数字。 如果没有找到数字,则打印无 - Take an integer array and print only the numbers that are in consecutive orders of 3 in java. If no number is found, print none 如果数组中的连续数字相隔相同的大小,请输入 - Enter if consecutive numbers in an array are separated by the same magnitude Java 递归 function 判断两个连续数是否相等 - Java Recursive function that determines if there are two consecutive numbers which are equal O(n)算法找到从1到n(非奇数)的连续整数数组中的奇数输出 - O(n) algorithm to find the odd-number-out in array of consecutive integers from 1 to n(not odd numbers) 仅使用 1 将两个整数相加 - Adding two integers together using only 1's
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM