英文:
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'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
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 –
答案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 -> 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.
答案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<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
答案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("\\d*77\\d*|\\d*7\\d7\\d*");
System.out.println(flag);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论