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

huangapple go评论80阅读模式
英文:

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

问题

import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {
        Integer a[] = { 1, 2, 1, 7, 3, 5, 2, 7 };
        boolean flag = IntStream.range(0, a.length - 1)
                .anyMatch(i -> (a[i] == 7 && a[i + 1] == 7) || (i < a.length - 2 && a[i] == 7 && a[i + 2] == 7));
        System.out.println(flag);
    }
}
英文:
Given an array of integers, return true if the array contains two 7&#39;s next to each other, or there are two 7&#39;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

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 &lt; a.length - 1; i++) {
			if (a[i] == 7 &amp;&amp; a[i + 1] == 7 || a[i] == 7 &amp;&amp; a[i + 1] == 1 &amp;&amp; a[i + 2] == 7) {
				flag = true;
			}
		}
		System.out.println(flag);
	}

//is it possible to avoid the for loop, and solve this using java streams –

答案1

得分: 1

以下是翻译好的内容:

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

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

下次请直接在问题中提供您的代码,这样可以更容易地帮助您。

英文:

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 -&gt; a[i] == 7 &amp;&amp; a[i+1] ==7 || a[i] == 7 
                    &amp;&amp; a[i + 1] == 1 &amp;&amp; a[i + 2] == 7 );

And please next time provide your code in the question itself, to make helping you easier.

答案2

得分: 0

// 使用流来实现,您可以编写一个自定义的 Collector,并像这样使用:

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

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

// 对于 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
英文:

To do it with streams, you can write a custom Collector, and use it like this:

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

答案3

得分: 0

可能是一个不正统的方法来处理这个任务,但是将数字转换为字符串然后使用正则表达式如何?

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);
}
英文:

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(&quot;\\d*77\\d*|\\d*7\\d7\\d*&quot;);
    System.out.println(flag);
}

huangapple
  • 本文由 发表于 2020年10月12日 16:26:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64314207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定