Code Maintenance & Programming Rules

 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 OJ:1.Two Sum

原文題目連結:https://leetcode.com/problems/two-sum/
題目的意思是要從一列數字中找出兩個數字的總和等於指定的數字,但得指出此兩數字在原來陣列的位置為何。

例如:
指定的數字為:9
數列為:【1, 3, 4, 7, 9, 2, 10】。
因為只會找到 7 + 2 = 9,而7在索引[3]的位置,2在索引[5],所以答案為[3, 5]

而筆者一開始也是使用暴力法(Brute Force)來解題,解完後看了一下https://leetcode.com/problems/two-sum/solution/ 才知道還有Two-pass Hash Table與One-pass Hash Table等解法,於是自己理解後,用C++的語言來實作囉,底下列出連結給讀者參考。

Two-pass Hash Table:
https://gist.github.com/pinglunliao/4b0e75dfde7d39468449a8f569dbfdbb

One-pass Hash Table;
https://gist.github.com/pinglunliao/f2e024ba28aa414640e3ebd45eead40e

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

If you like this post, please click the ads on the blog or buy me a coffee. Thank you very much.

留言