-
-
Notifications
You must be signed in to change notification settings - Fork 245
[Sophia] Week4 Solution #428
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
2 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,36 @@ | ||
/** | ||
* @param {string} s | ||
* @return {boolean} | ||
*/ | ||
var isPalindrome = function (s) { | ||
// 1. 아래 방식은 O(n^2)에 수렴함 | ||
// s의 구성요소들을 소문자로 변환 후 배열에 넣고 | ||
// 현재 길이가 2 이상이라면 | ||
// shift === pop 이면 true 아니면 false | ||
|
||
// 2. 투 포인터 사용 | ||
|
||
let str = s.toLowerCase().replace(/[^a-z0-9]/g, ""); | ||
|
||
// 두 포인터 사용: 시작과 끝에서부터 비교 | ||
let left = 0; | ||
let right = str.length - 1; | ||
|
||
while (left < right) { | ||
if (str[left] !== str[right]) { | ||
return false; // 두 문자가 다르면 palindrome 아님 | ||
} | ||
|
||
left++; | ||
right--; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
/* | ||
1. 시간복잡도: O(n) | ||
- toLowerCase, replace, 포인터 사용에 O(n) 소요됨 | ||
2. 공간복잡도: O(n) | ||
- 변수 str이 O(n) 차지함 | ||
*/ |
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.
저도 비슷하게 풀었습니다!
추가로, left와 right 변수를 반복문 안에서만 사용하므로, 굳이 미리 초기화하지 않고 바로 반복문에서 선언하는 방법도 있다는 생각을 해봅니다. 다만 가독성을 고려했을 때 (개인적으로) 현재 작성하신 코드가 더 좋다는 의견입니다! 😊