置頂

我的 VPython 教學文件 (HackMD 版本)

VPython 教學文件目錄 安裝及測試 基本語法 等速度直線運動 自由落下 終端速度 水平抛射 使用For迴圈計算水平抛射資料 斜向抛射 圓周運動 簡諧運動 單擺 木塊彈簧系統分離 重力及簡諧 行星運動 相疊木塊 雙重簡諧運動 一維彈性碰撞 ...

熱門文章

2026年9月2日 星期三

LeetCode 解題筆記:3875. Construct Uniform Parity Array I

作者:王一哲
日期:2026年9月2日


LeetCode 題目連結:3875. Construct Uniform Parity Array I

解題想法


簡單題,直接回傳 True。如果 $nums1$ 的數字皆為奇數或偶數,直接複製 $nums1$ 到 $nums2$ 就能符合條件。如果 $nums1$ 的數字同時有奇數及偶數,一定能將 $nums2$ 全部湊成奇數,有兩種狀況:
  1. $nums1[i]$ 是奇數,直接複製到 $nums2[i]$。
  2. $nums1[i]$ 是偶數,一定能找到一個是奇數的 $nums1[j]$,將 $nums1[i] - nums1[j]$ 複製到 $nums2[i]$。
結論:題目的要求一定成立。

Python 程式碼


Runtime: 0 ms, beats 100.00%. Memory: 19.23 MB, beats 47.47%.
class Solution:
    def uniformArray(self, nums1: list[int]) -> bool:
        return True


C++ 程式碼


Runtime: 0 ms, beats 100.00%. Memory: 30.46 MB, beats 7.23%.
class Solution {
public:
    bool uniformArray(vector<int>& nums1) {
        return true;
    }
};


沒有留言:

張貼留言