This guide outlines essential best practices spanning code style, architectural design, debugging, testing, performance, and portability—all aimed at reducing the long-term cognitive load of code maintenance. 🎨 1. Style Code is written for humans to read, and only incidentally for computers to execute. Variable Naming : Use descriptive names for global variables, and short names for local variables. Precision and Consistency : Use active names for functions (e.g., calculateTotal ). Above all, keep your coding style consistent throughout the project. Structure & Expressions : Use a consistent indentation and brace ( {} ) style to show program structure visually. Use the natural form for expressions. Use parentheses to make the semantics unambiguous. Break up overly complex expressions to keep them clear. Side Effects & Macros : Beware of functions with side effects. Avoid function-like macros; if unavoidable, parenthesize the macro body and arguments carefully. Magic Numbe...
LeetCode 解題練習:Richest Customer Wealth
- 取得連結
- X
- 以電子郵件傳送
- 其他應用程式
若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。
If you like this post, please click the ads on the blog or buy me a coffee. Thank you very much.
題目原文描述 https://leetcode.com/problems/richest-customer-wealth/description/
中文描述
給定一個 m * n 的陣列 accounts ,accounts[i][j] 是第 i 個客戶在第 j 個銀行裡的存款金額。請找出最富有的客戶。客戶的財富是將每間銀行的存款加總起來。
範例一:
輸入 accounts = [ [2, 3, 5], [7, 1, 9] ]
輸出 17
因為第一個客戶財富為 2 + 3 + 5 = 10
因為第二個客戶財富為 7 + 1 + 9 = 17
範例:
輸入 accounts = [ [2, 3, 5], [7, 1, 9], [11, 13, 2]
輸出 26
因為第一個客戶財富為 2 + 3 + 5 = 10
因為第二個客戶財富為 7 + 1 + 9 = 17
因為第三個客戶財富為 11 + 13 + 2 = 26
解法:
使用迴圈將每一列的數值加總存到 wealth,並與目前最大財富 maxWealth 比較,若大於 maxWealth 則 maxWealth 等於 wealth 。
Python Code
class Solution:
def maximumWealth(self, accounts: List[List[int]]) -> int:
maxWealth = 0
for account in accounts:
wealth = 0
for w in account:
wealth = wealth + w
if wealth > maxWealth:
maxWealth = wealth
return maxWealth
這個網誌中的熱門文章
Solutions to Blocky Game Music (Blockly 音樂遊戲參考解法)
Solutions to Blocky Game Movie (Blockly 影片遊戲參考解法)
If you like this post, please click the ads on the blog or buy me a coffee . Thank you very much. Level 1: Level 2: Level 3: Level 4: Level 5: Level 6: Level 7: Level 8: Level 9: 若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。 If you like this post, please click the ads on the blog or buy me a coffee . Thank you very much.
留言