计算三胞胎-首次进场 [英] Count triplets - First Approch

查看:86
本文介绍了计算三胞胎-首次进场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在HackerRank上进行此练习:

I'm doing this exercise on HackerRank:

为您提供了一个数组,您需要查找索引(i,j,k)的三元组数,以使在给定的公共比率r和i<时,这些索引处的元素呈几何级数.&

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/问题我的编译器有问题.这是代码:

Full exercise: https://www.hackerrank.com/challenges/count-triplets-1/problem I have problems with compiler. This is code:

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

(我从编程开始,所以我不知道代码是否正确)

(I'm starting out with programming, so I don't know if the code is correct)

=============================================================================

=============================================================================

更新:我没有错误,但每个测试用例返回0.

UPDATE: I don't have errors, but return 0 for each test cases.

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

推荐答案

您的问题是您要混淆两个相似但最终是非常不同的概念.

Your problem is that you are mixing up two similar, but in the end: very different concepts.

static long countTriplets(List<Long> arr, long r) {

似乎您想将 arr 视为 array .因为稍后您将使用数组访问语法 arr [someIndex] .

It seems you want to think of arr as of array. Because you later use the array access syntax arr[someIndex].

但是,您并没有将其声明为 array .您在那里有一个列表.

But well, you didn't declare it as array. You have a List there.

然后,您必须使用以下内容:

Then you have to use things like:

someList.getSize()

获取列表的 length ,或

someList.get(someIndex)

获取该列表中的特定元素.

to acquire a specific element in that list.

但是真正的答案是:当您不了解这样的基本基本知识时,然后从某个地方获取一些代码,然后将其转换为hackerrank,这不是您现在应该做的.

But the real answer is: when you don't understand such fundamental basic stuff, then fetching some code from somewhere, to turn it to hackerrank isn't what you should be doing right now.

含义:为了成功编写自己的代码,以解决此类问题,您需要已经了解该语言的足够多".

Meaning: in order to successfully write your own code, to solve such exercises, you need to understand "enough" of the language already.

因此:退后一步,并获得一本有关Java的好书.并通读那本书.

Thus: step back, and get a good book about Java. And work through that book.

这篇关于计算三胞胎-首次进场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆