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评论94阅读模式
英文:

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

问题

  1. import java.util.stream.IntStream;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Integer a[] = { 1, 2, 1, 7, 3, 5, 2, 7 };
  5. boolean flag = IntStream.range(0, a.length - 1)
  6. .anyMatch(i -> (a[i] == 7 && a[i + 1] == 7) || (i < a.length - 2 && a[i] == 7 && a[i + 2] == 7));
  7. System.out.println(flag);
  8. }
  9. }
英文:
  1. 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**
  2. Example1: [1, 7, 7] true
  3. Example2: [1, 7, 1, 7] true
  4. Example3: [1, 7, 1, 1, 7] false
  5. Example4: [7, 7, 1, 1, 7] true
  6. Example5: [9, 0, 5, 1, 7] false
  7. 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

  1. public void static void main(String[] args) {
  2. Integer a[] = { 1, 2, 1, 7, 3, 5, 2, 7 };
  3. boolean flag = false;
  4. for (int i = 0; i &lt; a.length - 1; i++) {
  5. 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) {
  6. flag = true;
  7. }
  8. }
  9. System.out.println(flag);
  10. }

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

答案1

得分: 1

以下是翻译好的内容:

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

  1. IntStream.range(0, a.length-1)
  2. .anyMatch( i -> a[i] == 7 && a[i+1] == 7 || a[i] == 7
  3. && 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.

  1. IntStream.range(0, a.length-1)
  2. .anyMatch( i -&gt; a[i] == 7 &amp;&amp; a[i+1] ==7 || a[i] == 7
  3. &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

  1. // 使用流来实现,您可以编写一个自定义的 Collector,并像这样使用:
  2. // 对于 int[]
  3. boolean flag = Arrays.stream(arr).boxed().collect(TwoSevens.collector());
  4. // 对于 Integer[]
  5. boolean flag = Arrays.stream(arr).collect(TwoSevens.collector());
  6. // 对于 List<Integer>
  7. boolean flag = list.stream().collect(TwoSevens.collector());
  8. class TwoSevens {
  9. public static Collector<Integer, ?, Boolean> collector() {
  10. return Collector.of(TwoSevens::new, TwoSevens::add,
  11. (a,b) -> { throw new UnsupportedOperationException("Parallel processing not supported"); },
  12. TwoSevens::getResult);
  13. }
  14. private int prev1, prev2;
  15. private boolean result;
  16. private TwoSevens() {/*nothing to do*/}
  17. private void add(int value) {
  18. if (value == 7 && (prev1 == 7 || prev2 == 7))
  19. this.result = true;
  20. prev1 = prev2;
  21. prev2 = value;
  22. }
  23. private boolean getResult() {
  24. return this.result;
  25. }
  26. }
  27. // 测试
  28. int[][] tests = { { 1, 7, 7 },
  29. { 1, 7, 1, 7 },
  30. { 1, 7, 1, 1, 7 },
  31. { 7, 7, 1, 1, 7 },
  32. { 9, 0, 5, 1, 7 },
  33. { 7, 7, 7, 7, 7 } };
  34. for (int[] arr : tests) {
  35. boolean flag = Arrays.stream(arr).boxed().collect(TwoSevens.collector());
  36. System.out.println(Arrays.toString(arr) + ": " + flag);
  37. }
  38. // 输出
  39. [1, 7, 7]: true
  40. [1, 7, 1, 7]: true
  41. [1, 7, 1, 1, 7]: false
  42. [7, 7, 1, 1, 7]: true
  43. [9, 0, 5, 1, 7]: false
  44. [7, 7, 7, 7, 7]: true
英文:

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

  1. // With an int[]
  2. boolean flag = Arrays.stream(arr).boxed().collect(TwoSevens.collector());
  3. // With an Integer[]
  4. boolean flag = Arrays.stream(arr).collect(TwoSevens.collector());
  5. // With a List&lt;Integer&gt;
  6. boolean flag = list.stream().collect(TwoSevens.collector());
  1. class TwoSevens {
  2. public static Collector&lt;Integer, ?, Boolean&gt; collector() {
  3. return Collector.of(TwoSevens::new, TwoSevens::add,
  4. (a,b) -&gt; { throw new UnsupportedOperationException(&quot;Parallel processing not supported&quot;); },
  5. TwoSevens::getResult);
  6. }
  7. private int prev1, prev2;
  8. private boolean result;
  9. private TwoSevens() {/*nothing to do*/}
  10. private void add(int value) {
  11. if (value == 7 &amp;&amp; (prev1 == 7 || prev2 == 7))
  12. this.result = true;
  13. prev1 = prev2;
  14. prev2 = value;
  15. }
  16. private boolean getResult() {
  17. return this.result;
  18. }
  19. }

Test

  1. int[][] tests = { { 1, 7, 7 },
  2. { 1, 7, 1, 7 },
  3. { 1, 7, 1, 1, 7 },
  4. { 7, 7, 1, 1, 7 },
  5. { 9, 0, 5, 1, 7 },
  6. { 7, 7, 7, 7, 7 } };
  7. for (int[] arr : tests) {
  8. boolean flag = Arrays.stream(arr).boxed().collect(TwoSevens.collector());
  9. System.out.println(Arrays.toString(arr) + &quot;: &quot; + flag);
  10. }

Output

  1. [1, 7, 7]: true
  2. [1, 7, 1, 7]: true
  3. [1, 7, 1, 1, 7]: false
  4. [7, 7, 1, 1, 7]: true
  5. [9, 0, 5, 1, 7]: false
  6. [7, 7, 7, 7, 7]: true

答案3

得分: 0

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

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

Probably an unorthodox way to approach this task but how about converting the numbers to string and using regex?

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

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:

确定