Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1512. Number of Good Pairs #74

Closed
twy30 opened this issue Nov 3, 2020 · 3 comments · Fixed by #90
Closed

1512. Number of Good Pairs #74

twy30 opened this issue Nov 3, 2020 · 3 comments · Fixed by #90
Labels

Comments

@twy30
Copy link
Contributor

twy30 commented Nov 3, 2020

https://leetcode.com/problems/number-of-good-pairs/

using System.Collections.Generic;
using System.Linq;

public class Solution
{
    public int NumIdenticalPairs(int[] nums)
    {
        // 「輸入的數字」(複數)
        var inputNumbers = nums;

        // 「『輸入的數字』的數量」(複數)
        var inputNumberCounts = new Dictionary<int, int>();

        foreach (var i in inputNumbers)
        {
            if (inputNumberCounts.ContainsKey(i))
            {
                ++inputNumberCounts[i];
            }
            else
            {
                inputNumberCounts[i] = 1;
            }
        }

        return inputNumberCounts.Sum(count => count.Value * (count.Value - 1) / 2);
    }
}

請參考「刷 LeetCode 練習命名」 #69 😊

@LPenny-github
Copy link

又來麻煩大大,尋求命名建議了 orz
感激不盡 orz

public int IdenticalPairsCount(int[] inputNumbers)
        {
            int numberOfIdenticalPairs = 0;
            
            for (int index = 0; index < inputNumbers.Length-1; ++index)
            {
                for (int nextIndex = index+1; nextIndex< inputNumbers.Length; ++nextIndex)
                {
                    if (inputNumbers[index] == inputNumbers[nextIndex] )
                    {
                        ++numberOfIdenticalPairs;
                    }
                }
            }

            return numberOfIdenticalPairs;
        }

@twy30
Copy link
Contributor Author

twy30 commented Nov 18, 2020

@LPenny-github

堅持「刻意練習」,一定會進步的 💪


            int numberOfIdenticalPairs = 0;

這個命名很好懂 👍

另外可能的寫法有 identicalPairCount, quantityOfIdenticalPairs 等組合。


            for (int index = 0; index < inputNumbers.Length-1; ++index)
            {
                for (int nextIndex = index+1; nextIndex< inputNumbers.Length; ++nextIndex)

這個地方,或許可以用 i, j 來代替 index, nextIndex 🤔

我的想法是:

  • i, j (還有 k) 算是軟體業常用來表達 索引值 變數的名稱
  • 這裡的迴圈結構很簡單(simple)
  • LeetCode 題目中有使用 i, j (抄錄如下) ;有讀過原題目的讀者應該能聯想到
    • A pair (i,j) is called good if nums[i] == nums[j] and i < j.

不過,當作練習,如果我們就是不想要用 i, j, 那麼,要怎麼命名這兩個索引值變數?

我想我會沿用 LeetCode 題目中的用詞 "good pair", 然後寫成

  • goodPairIndex1
  • goodPairIndex2

  • goodPairIndexI
  • goodPairIndexJ

🤔

@LPenny-github
Copy link

@twy30 感謝大大的鼓勵和指教 orz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

2 participants