Python 基礎知識簡單

數組中的最小元素

“數組中最小元素”問題的詳細指南和 Python 實作。

問題陳述

簡單

寫一個函數 find_smallest(arr),使用遞迴尋找並傳回給定陣列 arr 中的最小元素。函數接受一個整數列表作為輸入並傳回一個整數 - 列表中的最小值。

約束條件
  • 1 <= len(arr) <= 1000
  • -10^6 <= arr[i] <= 10^6

範例

Example 1
Input
arr = [1, 4, 3, -5, 15]
Output
-5
Explanation

Among all elements, -5 is the smallest.

Example 2
Input
arr = [10, 20, 5, 8]
Output
5
Explanation

5 is the minimum value in the array.

Example 3
Input
arr = [-3, -7, -1, -9]
Output
-9
Explanation

Among all negative numbers, -9 is the smallest.

Need a Hint?
考慮使用特定於遞歸的資料結構,例如集合或堆。
Edge Cases to Watch
  • 空輸入結構
  • 單元素輸入
  • 大數值範圍

準備好解決了嗎?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在編輯器中開啟
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推薦的 Python 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。