我正在 HackerRank 上进行此练习:
You are given an array and you need to find number of triplets of indices (i, j, k) such that the elements at those indices are in geometric progression for a given common ratio r and i < j < k.
完整练习:https://www.hackerrank.com/challenges/count-triplets-1/problem 我的编译器有问题。 这是代码:
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
public class Solution {
// Complete the countTriplets function below.
static long countTriplets(List<Long> arr, long r) {
int counter = 0;
for (int i = 0; i < arr.length() - 2; i++) {
for (int j = i + 1; j < arr.length() - 1; j++) {
if (arr[j] - arr[i] == r) {
for (int k = j + 1; k < arr.length(); k++) {
if (arr[k] - arr[j] == r) {
System.out.println("(" + arr[i] + "," + arr[j] + "," + arr[k] + ")");
counter++;
break;
}
}
}
}
}
;
return counter;
}
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] nr = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int n = Integer.parseInt(nr[0]);
long r = Long.parseLong(nr[1]);
List<Long> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")).map(Long::parseLong)
.collect(toList());
long ans = countTriplets(arr, r);
bufferedWriter.write(String.valueOf(ans));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
我有这个错误:
Solution.java:19: error: cannot find symbol
for (int i = 0; i < arr.length() -2; i++){
^
symbol: method length()
location: variable arr of type List<Long>
Solution.java:20: error: cannot find symbol
for(int j = i + 1; j < arr.length() -1; j++){
^
symbol: method length()
location: variable arr of type List<Long>
Solution.java:21: error: array required, but List<Long> found
if(arr[j] - arr[i] == r){
^
Solution.java:21: error: array required, but List<Long> found
if(arr[j] - arr[i] == r){
^
Solution.java:22: error: cannot find symbol
for(int k = j + 1; k < arr.length(); k++){
^
symbol: method length()
location: variable arr of type List<Long>
Solution.java:23: error: array required, but List<Long> found
if(arr[k] - arr[j] == r){
^
Solution.java:23: error: array required, but List<Long> found
if(arr[k] - arr[j] == r){
^
Solution.java:24: error: array required, but List<Long> found
System.out.println("(" +arr[i] + "," +arr[j] + "," +arr[k] +")");
^
Solution.java:24: error: array required, but List<Long> found
System.out.println("(" +arr[i] + "," +arr[j] + "," +arr[k] +")");
^
Solution.java:24: error: array required, but List<Long> found
System.out.println("(" +arr[i] + "," +arr[j] + "," +arr[k] +")");
^
10 errors
(我刚开始编程,所以我不知道代码是否正确)
================================================== ===============================
更新:我没有错误,但每个测试用例返回 0。
public class Solution {
// Complete the countTriplets function below.
static long countTriplets(List<Long> arr, long r) {
// Long[] a = arr.toArray();
int counter = 0;
for (int i = 0; i < arr.size() - 2; i++) {
for (int j = i + 1; j < arr.size() - 1; j++) {
if (arr.get(j) - arr.get(i) == r) {
for (int k = j + 1; k < arr.size(); k++) {
if (arr.get(k) - arr.get(j) == r) {
System.out.println("(" + arr.get(i) + "," + arr.get(j) + "," + arr.get(k) + ")");
counter++;
break;
}
}
}
}
}
return counter;
}
}
最佳答案
变量 arr 的输入类型是列表,而不是整数数组。 List是Collection的一个接口(interface)。
int[] arr = new int[];//是声明和数组的方式,任何元素都可以使用 arr[index] 访问
列表可以包含重复项,并且元素是有序的。 示例实现是 LinkedList(基于链接列表)和 ArrayList(基于动态数组)。
arr.size() 是获取集合大小的函数。此外,为了访问列表中的任何元素,使用了 arr.get(index) 函数。
关于java - 数三元组 - 第一种方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58932807/