-
-
Notifications
You must be signed in to change notification settings - Fork 246
[혜준] Week2 문제 풀이 #351
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 문제 풀이 #351
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ac0d67a
Valid Anagram
hyejjun 4ed4d3c
Merge branch 'DaleStudy:main' into main
hyejjun 205eb5d
Counting Bits
hyejjun 59901d1
line break 추가
hyejjun 8a5c984
시공간 복잡도 주석에 추가
hyejjun d6b6770
Merge branch 'DaleStudy:main' into main
hyejjun c67f669
Num Decodings
hyejjun 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,31 @@ | ||
/** | ||
* @param {number} n | ||
* @return {number[]} | ||
*/ | ||
var countBits = function (n) { | ||
|
||
let result = []; | ||
|
||
for (let i = 0; i <= n; i++) { | ||
|
||
let binary = i.toString(2); | ||
let sum = 0; | ||
|
||
for (let char of binary) { | ||
sum += Number(char); | ||
} | ||
result.push(sum); | ||
} | ||
|
||
return result; | ||
|
||
}; | ||
|
||
|
||
console.log(countBits(2)); | ||
console.log(countBits(5)); | ||
|
||
/* | ||
시간 복잡도: O(n * log n) | ||
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. 안녕하세요~ 어떤 이유로 시간 복잡도가 n log n 이 나오는지, 여기서 n은 무엇을 가리키는지 설명해주시면 더 좋을 것 같아요! 나중에 실제 코딩 테스트 면접에서도 도움이 될 거라고 생각합니다 |
||
공간 복잡도: O(n) | ||
*/ |
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,42 @@ | ||
/** | ||
* @param {string} s | ||
* @return {number} | ||
*/ | ||
|
||
var numDecodings = function (s) { | ||
if (s == null || s.length === 0) { | ||
return 0; | ||
} | ||
|
||
const n = s.length; | ||
const dp = new Array(n + 1).fill(0); | ||
dp[0] = 1; | ||
dp[1] = s[0] === '0' ? 0 : 1; | ||
|
||
for (let i = 2; i <= n; i++) { | ||
const oneDigit = parseInt(s.substring(i - 1, i)); | ||
const twoDigits = parseInt(s.substring(i - 2, i)); | ||
|
||
// Check if single digit decoding is possible | ||
if (oneDigit >= 1 && oneDigit <= 9) { | ||
dp[i] += dp[i - 1]; | ||
} | ||
|
||
// Check if two digit decoding is possible | ||
if (twoDigits >= 10 && twoDigits <= 26) { | ||
dp[i] += dp[i - 2]; | ||
} | ||
} | ||
|
||
return dp[n]; | ||
}; | ||
|
||
|
||
console.log(numDecodings("12")); | ||
console.log(numDecodings("226")); | ||
console.log(numDecodings("06")); | ||
|
||
/* | ||
시간 복잡도: O(n) | ||
공간 복잡도: O(n) | ||
*/ |
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,33 @@ | ||
/** | ||
* @param {string} s | ||
* @param {string} t | ||
* @return {boolean} | ||
*/ | ||
var isAnagram = function (s, t) { | ||
if (s.length !== t.length) return false; | ||
|
||
let countS = {}; | ||
let countT = {}; | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
countS[s[i]] = (countS[s[i]] || 0) + 1; | ||
countT[t[i]] = (countT[t[i]] || 0) + 1; | ||
} | ||
|
||
for (let key in countS) { | ||
if (countS[key] !== countT[key]) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
|
||
}; | ||
|
||
console.log(isAnagram("anagram", "nagaram")); | ||
console.log(isAnagram("rat", "car")); | ||
|
||
/* | ||
시간 복잡도: O(n) | ||
공간 복잡도: O(n) | ||
*/ |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
저는 재할당 되지 않을 변수는 const 키워드로 선언하는 편인데요. 이는 코드를 읽는 사람으로 하여금 이 변수는 재할당되지 않을 것임을 예측할 수 있도록 해주고, 나중에 실수로 해당 변수에 다른 값을 할당하게 되는 버그를 사전에 예방할 수 있기 때문이에요.