簡単 2件解決済み 正答率100.0%

Count Positive Numbers

Count the values greater than zero in a list.

このチャレンジで学べること

  • Loop over a list of numbers
  • Count values that match a condition
  • Ignore zero and negatives

問題

翻訳を表示

Count how many numbers in the list are greater than zero. Zero is neither positive nor negative.

Implement count_positives(numbers).

Input

numbers: The list of integers.

Output

Return the count.

制約

0 ≤ length ≤ 100,000; each value fits in a 64-bit integer

例

例 1
入力:
numbers = [3, -1, 0, 7, 2]

期待される出力:
expected = 3
入力:
std::vector<long long> numbers = {3, -1, 0, 7, 2};

期待される出力:
long long expected = 3;
入力:
long[] numbers = new long[] {3, -1, 0, 7, 2};

期待される出力:
long expected = 3;

解説

3, 7 and 2 are positive: 3.

例 2
入力:
numbers = [0, 0, -5]

期待される出力:
expected = 0
入力:
std::vector<long long> numbers = {0, 0, -5};

期待される出力:
long long expected = 0;
入力:
long[] numbers = new long[] {0, 0, -5};

期待される出力:
long expected = 0;

解説

No value is above zero: 0.

解き方のヒント

まずは自分で考えてから、ヒントを1つずつ開いてみましょう。

ヒント 1

Only numbers greater than zero should be counted.

ヒント 2

The task is just a filtered count over the list.

ヒント 3

Do not include zero, since it is neither positive nor negative.

目安の計算量

O(n) time, O(1) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

# コーダー 言語 時間 メモリ AI 評価 解答
1 LMLaura Mahlow Python 798 ms - 5/5 コードを見る
2 RCRiccardo Corti Python 809 ms - 5/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意: この下に解答が2件あります まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。