-
-
Notifications
You must be signed in to change notification settings - Fork 195
[호돌이] Week2 #729
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
[호돌이] Week2 #729
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4cffb45
valid anagram solution
yeeZinu 8a98f47
Merge branch 'main' of https://github.com/yeeZinu/leetcode-study
yeeZinu 1dea909
climbling stairs soltion
yeeZinu 9ce43d1
3sum solution
yeeZinu 5d7a3f8
Merge branch 'DaleStudy:main' into main
yeeZinu 06a920f
Merge branch 'main' of https://github.com/yeeZinu/leetcode-study
yeeZinu 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,54 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number[][]} | ||
* | ||
* 문제: 세 수를 더해서 0을 만들어야함. | ||
* | ||
* 주의사항: 인덱스 상관없어 세 수의 조합이 같으면 안됨. | ||
* 핵심: 배열을 오름차순으로 정렬해서 | ||
* 양 끝에 각 인덱스넣고 사이에 있는인덱스로 계속 더하면서 | ||
* 좌우 인덱스를 0에 가깝게 +- 하면된다~ | ||
* | ||
*/ | ||
var threeSum = function (nums) { | ||
// 결과를 저장할 배열 | ||
let result = []; | ||
// 주어진 수를 오름차순으로 정렬 | ||
nums.sort((a, b) => a - b); | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
// nums[i] > 0보다 크다면? 반복 끝 | ||
if (nums[i] > 0) { | ||
break; | ||
} | ||
|
||
let j = i + 1; // 중간에서 바뀔 인덱스 | ||
let k = nums.length - 1; // 맨 마지막에서 부터 움직일 인덱스 | ||
|
||
while (j < k) { | ||
let sum = nums[i] + nums[j] + nums[k]; | ||
|
||
// 총합이 양수라면 k인덱스 한칸뒤로 ㄱ | ||
if (sum > 0) { | ||
k--; | ||
} | ||
// 음수라면 j진행 ㄱ | ||
else if (sum < 0) { | ||
j++; | ||
} | ||
// 0이면 result배열에 추가, j진행 | ||
else { | ||
result.push([nums[i], nums[j], nums[k]]); | ||
j++; | ||
|
||
// j가 이전값과 같다면 무시하고 진행하기 | ||
while (nums[j] === nums[j - 1] && j < k) { | ||
j++; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
|
||
}; |
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,21 @@ | ||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var climbStairs = function (n) { | ||
// n+1 배열을 만들고 0으로 초기화 | ||
const dp = new Array(n + 1).fill(0); | ||
|
||
// 인덱스 0번과 1번은 1로 초기화 | ||
dp[0] = 1; | ||
dp[1] = 1; | ||
|
||
// 이전계단과 그 이전 계단의 합이 계단을 올라갈 수 있는 총합 | ||
for (let i = 2; i <= n; i++) { | ||
dp[i] = dp[i - 1] + dp[i - 2]; | ||
} | ||
|
||
return dp[n]; | ||
}; | ||
|
||
console.log(climbStairs(5)); |
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,77 @@ | ||
/** | ||
* @param {string} s | ||
* @param {string} t | ||
* @return {boolean} | ||
* | ||
*/ | ||
|
||
var isAnagram = function (s, t) { | ||
// 두 문자열의 길이가 같지않다면 false | ||
if (s.length !== t.length) { | ||
return false; | ||
}; | ||
|
||
// Map을 사용해서 데이터 최신화 | ||
let count = new Map(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. map 대신 알파벳 만큼인 26짜리 배열 하나만을 사용해서 풀이하는 방법도 있을것 같아요! |
||
|
||
// set으로 해당 문자를 key, 갯수를 value로 카운트 | ||
for (let char of s) { | ||
count.set(char, (count.get(char) || 0) + 1) | ||
} | ||
|
||
for (let char of t) { | ||
// 새로운 문자가 map에 없거나, 이미 카운트가 0이라면 false | ||
if (!count.has(char) || count.get(char) === 0) { | ||
return false; | ||
} | ||
// 문자 카운트 -1 | ||
count.set(char, (count.get(char) - 1)) | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/* | ||
* 문제: 두 문자열의 구성요소 비교 | ||
* | ||
* 생각: 각 문자열에 상대 문자열의 스펠링이 있는지 확인하고 splice로 제거 | ||
* 결과: 타임리밋 | ||
var isAnagram = function (s, t) { | ||
if (s.length !== t.length) { | ||
return false; | ||
}; | ||
|
||
let sArr = []; | ||
let tArr = []; | ||
let finder = 0; | ||
|
||
for (let j = 0; j < s.length; j++) { | ||
sArr.push(s[j]); | ||
tArr.push(t[j]); | ||
} | ||
|
||
// 반복문을 돌며 s와 t를 비교 | ||
for (let i = 0; i < s.length; i++) { | ||
//s[i]번 째가 t안에 있다면? | ||
if (tArr.indexOf(sArr[0]) !== -1) { | ||
// 찾은 인덱스를 저장해 줌 -> sArr 에서 splice먼저해버리면 배열이 달라져서 못찾음;; | ||
finder = tArr.indexOf(sArr[0]); | ||
//s[i]를 스플라이스로 빼고 | ||
sArr.splice(0, 1); | ||
//t의 인덱스번호를 슬라이스로 뺌 | ||
tArr.splice(finder, 1); | ||
} | ||
console.log(i); | ||
console.log(sArr); | ||
console.log(tArr); | ||
} | ||
|
||
// s와 t의 길이가 0이면 true 아니면 fales | ||
if (sArr.length === 0 && tArr.length === 0) { | ||
return true; | ||
} | ||
else return false | ||
|
||
}; | ||
|
||
*/ |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
배열 선언에서
.fill(0)
부분은 없어도 괜찮지 않을까 궁금합니다!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
정수가 들어갈 자리라서 미리 0으로 초기화했었는데 안해도 상관은 없을거같긴 합니다.
한번 찾아보겠습니다!