1426. Counting Elements

LeetCode easy original: C# #array #csharp #easy #leetcode
Der Aufgabentext wird für die gewählte Sprache aus dem Russischen übersetzt. Code bleibt unverändert.

Дан Ganzzahl Array arr, посчитайте, сколько elementов x в нем есть таких, что x + 1 также находится в arr. Если в arr есть дубликаты, считайте их отдельно.

Beispiel:

Input: arr = [1,2,3]

Output: 2

Explanation: 1 and 2 are counted cause 2 and 3 are in arr.

C# Lösung

zugeordnet/original
public class Solution {
    public int CountElements(int[] arr) {
        int count = 0;
        foreach (int x in arr) {
            if (IntegerInArray(arr, x + 1)) {
                count++;
            }
        }
        return count;
    }
    
    public bool IntegerInArray(int[] arr, int target) {
        foreach (int x in arr) {
            if (x == target) {
                return true;
            }
        }
        return false;
    }
}

C++ Lösung

Auto-Entwurf, vor dem Einreichen prüfen
#include <bits/stdc++.h>
using namespace std;

// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
    public int CountElements(vector<int>& arr) {
        int count = 0;
        foreach (int x in arr) {
            if (IntegerInArray(arr, x + 1)) {
                count++;
            }
        }
        return count;
    }
    
    public bool IntegerInArray(vector<int>& arr, int target) {
        foreach (int x in arr) {
            if (x == target) {
                return true;
            }
        }
        return false;
    }
}

Java Lösung

zugeordnet/original
class Solution {
    public int countElements(int[] arr) {
        int count = 0;
        for (int x : arr) {
            if (integerInArray(arr, x + 1)) {
                count++;
            }
        }
        return count;
    }

    public boolean integerInArray(int[] arr, int target) {
        for (int x : arr) {
            if (x == target) {
                return true;
            }
        }
        return false;
    }
}

JavaScript Lösung

zugeordnet/original
class Solution {
    countElements(arr) {
        let count = 0;
        for (let x of arr) {
            if (this.integerInArray(arr, x + 1)) {
                count++;
            }
        }
        return count;
    }

    integerInArray(arr, target) {
        for (let x of arr) {
            if (x === target) {
                return true;
            }
        }
        return false;
    }
}

Python Lösung

zugeordnet/original
class Solution:
    def countElements(self, arr: List[int]) -> int:
        def integerInArray(arr, target):
            for x in arr:
                if x == target:
                    return True
            return False
        
        count = 0
        for x in arr:
            if integerInArray(arr, x + 1):
                count += 1
        return count

Go Lösung

zugeordnet/original
package main

func countElements(arr []int) int {
    count := 0
    for _, x := range arr {
        if integerInArray(arr, x+1) {
            count++
        }
    }
    return count
}

func integerInArray(arr []int, target int) bool {
    for _, x := range arr {
        if x == target {
            return true
        }
    }
    return false
}

Algorithm

Создайте вспомогательную функцию для проверки, содержится ли element в Arrayе.

Итерируйте по каждому elementу Arrayа и используйте вспомогательную функцию для проверки, содержится ли element x + 1 в Arrayе.

Увеличьте счетчик, если x + 1 найден, и return значение счетчика.

😎

Stellen zu dieser Aufgabe

aktive Stellen with overlapping task tags are angezeigt.

Alle Stellen
Es gibt noch keine aktiven Stellen.