繁体   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

请帮忙,我可以使用常规 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);
    }
 

//是否可以避免for循环,并使用java流解决这个问题 –

这是一个非常基本的答案。 这可能不是最聪明的解决方案,但我无法立即想到更好的解决方案。

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 );

下次请在问题本身中提供您的代码,以便更轻松地帮助您。

要使用流来做到这一点,您可以编写一个自定义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;
    }
}

测试

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);
}

输出

[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

可能是处理此任务的非正统方法,但是如何将数字转换为字符串并使用正则表达式?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM