-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Helena] Week 10 solutions #167
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
Conversation
yolophg
commented
Jul 3, 2024
•
edited
Loading
edited
- Graph Valid Tree
- Number of Connected Components in an Undirected Graph
- House Robber
- House Robber II
- Longest Palindromic Substring
// expand around the current character | ||
expandAroundCenter(i, i); | ||
// expand around the current and next character | ||
expandAroundCenter(i, i + 1); |
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.
같은 함수를 여기서 right를 1 더해서 한 번 더 호출하는 이유는 뭘까요?
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.
두 번째 호출에서 right를 1 더해서 호출하는 이유는 문자열 내의 두 가지 경우의 대칭 문자열을 모두 처리하기 위한 구현이에요!
expandAroundCenter(i, i) 호출은 홀수 길이의 대칭 문자열을 찾기 위한 것이고, expandAroundCenter(i, i + 1) 호출은 짝수 길이의 대칭 문자열을 위한 것입니다.
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.
오 길이별로 나눠서 진행을 하신거군요 ㅎㅎ
재밌는 접근인것 같습니다 : )
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.
var longestPalindrome = function (s) {
let maxPalindrome = '';
// to expand around the center and update the start and maxLength
function expandAroundCenter(center) {
let left = center;
let right = center;
while (right < s.length - 1 && s[center] === s[right + 1]) {
right++;
}
while (left > 0 && right < s.length - 1 && s[left - 1] === s[right + 1]) {
left--;
right++;
}
if (maxPalindrome.length < right - left + 1) {
maxPalindrome = s.substring(left, right + 1);
}
}
// iterate through each character in the string
for (let i = 0; i < s.length; i++) {
// expand around the current character
expandAroundCenter(i);
}
// return the longest palindromic substring
return maxPalindrome;
};
이렇게도 작성 가능하네요
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.
깔끔하게 코드 작성해주시고, 주석이 친절하게 추가되어 있어서 읽기 좋았습니다~ 고생하셨습니다!
if (root1 !== root2) { | ||
parent[root1] = root2; | ||
} |
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.
루트가 둘 중 하나이면 되니 여기에서는 크기 비교를 따로 하지 않아도 되는군요! union find 풀이법 흥미롭게 잘 보았습니다!