Skip to content

Commit 7673518

Browse files
feat:two-sum ํ’€์ด
1 parent 0f41411 commit 7673518

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

โ€Žtwo-sum/grapefruitgreentealoe.jsโ€Ž

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,30 @@ var twoSum3 = function (nums, target) {
4848
numMap.set(nums[i], i); // ๊ณต๊ฐ„ O(1)
4949
}
5050
};
51+
52+
53+
//25.7.24
54+
/**
55+
* @param {number[]} nums
56+
* @param {number} target
57+
* @return {number[]}
58+
*/
59+
var twoSum = function(nums, target) {
60+
//์šฐ์„  nums๋ฐฐ์—ด์—์„œ target๋ณด๋‹ค ํฐ๊ฒƒ์€ ์‚ญ์ œํ•œ๋‹ค. => ์ด๊ฒƒ ๋˜ํ•œ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋“ ๋‹ค. ํ•˜์ง€๋ง์ž
61+
//1. ํˆฌํฌ์ธํ„ฐ ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•œ๋‹ค.
62+
for(let i=0;i<nums.length-1;i++){
63+
let total = nums[i]
64+
for(let j=i+1;j<nums.length;j++){
65+
if(total+nums[j] == target){
66+
return [i,j]
67+
}
68+
}
69+
}
70+
};
71+
72+
/**
73+
* ์‹œ๊ฐ„๋ณต์žก๋„: O(N^2)
74+
* ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
75+
*/
76+
77+
//ํˆฌํฌ์ธํ„ฐ๋กœ ๋‹ค์‹œ ํ’€์—ˆ์ง€๋งŒ, ์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ Map์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ™œ์šฉํ•˜๋Š”๊ฒƒ.

0 commit comments

Comments
ย (0)