-
Notifications
You must be signed in to change notification settings - Fork 46
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
771. Jewels and Stones #75
Comments
再麻煩 @twy30 給予命名建議 orz (本來是因為大大上一題 #74 (comment) 的答案所以試著用 Dictionary,結果看到大家的答案覺得自己頗丟臉 XDDDD public class Solution {
public int NumJewelsInStones(string jewels, string stones) {
// 把 stones 做成字典
var stonesCategoryCount = new Dictionary<char, int>();
foreach (var i in stones)
{
if (stonesCategoryCount.ContainsKey(i))
{
++stonesCategoryCount[i];
}
else
{
stonesCategoryCount[i] = 1;
}
}
// 如果 jewels[i] == 字典裡的索引,那就加總該索引的值
int jewelsInStonesCount= 0;
for(int i = 0; i < jewels.Length; ++i)
{
if(stonesCategoryCount.ContainsKey(jewels[i]))
{
jewelsInStonesCount += stonesCategoryCount[jewels[i]];
}
}
return jewelsInStonesCount;
}
} |
每個人都是從零開始學起的 😊 var stonesCategoryCount = new Dictionary<char, int>();
int jewelsInStonesCount= 0;
|
我想大大應該內建想法是 「石頭 種類 (的) 數量 (複數)」和 「寶石 種類 (的) 數量」🤔,所以前者是複數,後者是單數? 另外,我也發現:
我以為疊字是 N (stones) + V (count) 這樣疊,但其實是 N (stone)+ N (counts) 感謝大大 orz |
我對前者的想法是:
我對後者的想法是:
這裡有兩個面向:「美式英文文法」與「寫程式會用到的英文」。 從英文文法「用單字組成新的字/片語」的角度來看,可以參考這兩篇:
從「程式英文」的角度來看,我們是要命名「這個變數/類別是代表『什麼樣的東西』」;而『什麼樣的東西』就分成「描述『什麼樣』的形容詞」與真正的「東西」本體。 這裡的 |
https://leetcode.com/problems/jewels-and-stones/
請參考「刷 LeetCode 練習命名」 #69 😊
The text was updated successfully, but these errors were encountered: