我們繼續透過 LeetCode #1 Two Sum 來實際感受解決問題的過程 ( 題目連結 )
使用哪種資料結構:Array
let len be the length of nums
let total be 0
for i (0 to len-1) do
for j (i+1 to len) do
total = nums[i] + nums[j]
if (total = target) than
return [i,j]
end if
end for
end for
const len = nums.length;
let total = 0;
for (let i=0 ; i<len-1 ; i++) {
for (let j=i+1 ; j<len ; j++) {
total = nums[i] + nums[j];
if (target === total) {
return [i, j]
}
}
}
原文連結:LeetCode:Two Sum ( 用 JavaScript 學演算法 ) - Ted's Point 泰德觀點