-
-
Notifications
You must be signed in to change notification settings - Fork 247
[minji0214]week1 solutions #1706
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
function containsDuplicate(nums: number[]): boolean { | ||
let answer = false | ||
for(let i = 0; i< nums.length; i ++ ){ | ||
if(nums.findIndex((value)=> value === nums[i]) !== i){ | ||
answer = true | ||
break; | ||
} | ||
} | ||
return answer | ||
}; | ||
//-> time limit 초과 오류 발생 | ||
//시간복잡도 | ||
// for + findIndex O(n²) ❌ | ||
// for + Set or Map O(n) ✅ | ||
//중복 여부만 체크할 때는 무조건 Set을 쓰는 게 효율적 | ||
// includes, findIndex, indexOf는 절대 루프 안에서 쓰지 말것 — O(n²) | ||
function containsDuplicate(nums: number[]): boolean { | ||
const seen = new Set(); //지금까지 본 숫자들을 저장 | ||
for (const num of nums) { | ||
if (seen.has(num)) return true; // 중복 발견 | ||
seen.add(num); // 중복이 아닐 경우 추가 | ||
} | ||
return false; // 중복 없음 | ||
} | ||
//Set : 중복 없는 값을의 모음. 배열처럼 생겼지만, 중복을 허용하지 않음 (중복제거) | ||
//Map : key → value 형태로 저장하는 객체 (검색 수정) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
function twoSum(nums: number[], target: number): number[] { | ||
let answer = [] | ||
for(let i = 0 ; i < nums.length; i ++){ | ||
let temp = target - nums[i] | ||
if (nums.includes(temp) && temp != nums[i]) { | ||
const index = nums.findIndex((test) => test === temp ) | ||
answer.push(index) | ||
} | ||
} | ||
return answer | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.