-
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
1470. Shuffle the Array #72
Comments
又來打擾了 XD public int[] Shuffle(int[] unmodifiedArray, int halfOfArrayLength)
{
//製作新容器 List
List<int> shffledArray = new List<int>(halfOfArrayLength*2);
for (int i = 0; i < halfOfArrayLength; ++i)
{
//每加原始陣列的一個數字後,插入一個 halfOfArrayLength 起算的數字
shffledArray.Add(unmodifiedArray[i]);
shffledArray.Add(unmodifiedArray[i + halfOfArrayLength]);
}
//將容器的格式轉回成 Array
return shffledArray.ToArray();
} |
實力就是這樣經由練習累積起來的 😊 public int[] Shuffle(int[] unmodifiedArray, int halfOfArrayLength)
List<int> shffledArray = new List<int>(halfOfArrayLength*2);
|
@twy30 首先,感謝大大的指教 orz
我其實不確定這三者的區別 😅:未更改過的 vs. 原始的 vs. 輸入的
但我覺得自己可能沒有搞懂三者真正的差別 🤔
當初是考量到盡量減少用字 🤔 因為,原始 inputArray / unmodifiedArray 和 outputArray / shuffledArray 是相同長度的。
對不起,一時不察 orz 下次會更注意 😆rz |
不客氣 😊
這裡沒有「唯一的正確答案」,就看你想要強調什麼語意、想要如何引導你的讀者。 在 #70 (comment) 裡, 回到這題來,如果「該 易言之, |
@twy30 感謝大大 orz |
教考卷! # ruby
def shuffle(input_nums, second_half_start_index)
first_half_end_index = second_half_start_index - 1
# add the first element in the first half and second half to result every time
(0..first_half_end_index).inject([]) do |result, index|
combie_nums = [input_nums[index], input_nums[second_half_start_index + index]]
result + combie_nums
end
end |
歡迎 😊 combie_nums = [input_nums[index], input_nums[second_half_start_index + index]] 我猜 從「是什麼(what)」的角度來看的話,這個變數也可以命名為:
另一個可能性,與題目中的 "shuffle" 稍微有連結:
可以參考看看 😊 |
@twy30 學習了! 想問個問題,一般在看到 |
我覺得不一定 🤔 例如說, C# 有 tuple 這個觀念,那習慣使用 C# 的讀者看到 tuple 或許會先想到 C# 的 tuple, 而非 array 。 反過來說,如果在某個專案/語言中,慣例是用 array 來實作 tuple, pair (甚至 list), 那或許讀者會第一時間聯想到 array 。
這大概無法一概而論 🤔 我想到這幾個方向:
等等。 |
https://leetcode.com/problems/shuffle-the-array/
請參考「刷 LeetCode 練習命名」 #69 😊
The text was updated successfully, but these errors were encountered: