Skip to content

[std-freejia] week 03 solutions #1805

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 1 commit into from
Aug 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions decode-ways/std-freejia.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public int numDecodings(String s) {
// 0 으로 시작하거나 빈 문자열이면 해독불가
int sLen = s.length();
if (sLen == 0 || s.charAt(0) == '0') return 0;

int[] dp = new int[sLen+1];

dp[0] = 1;
dp[1] = 1;

for (int i = 2; i <= sLen; i++) {

int num = Integer.parseInt(s.substring(i-2, i));
// 1자리 숫자로 해독 가능한 경우
if (s.charAt(i-1) != '0') {
dp[i] += dp[i-1];
}
// 2자리 숫자로 해독 가능한 경우
if (num >= 10 && num <= 26) {
dp[i] += dp[i-2];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1차원 dp 리스트를 이용한 O(n) space DP로 풀이하셨네요! dp[i]를 계산할 때 매 단계에서 dp[i - 1]과 dp[i - 2]만 확인하기 때문에, 이 값들을 두 개의 변수로 트래킹한다면 O(1) space DP로 최적화를 한 번 더 할 수 있을 것 같습니다~! 😀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오! 변수로 트래킹 하는 방법도 있네요. 재밌을 것 같아요. 피드백 감사합니다 !!

}
}
return dp[sLen];
}
}
10 changes: 10 additions & 0 deletions number-of-1-bits/std-freejia.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution {
public int hammingWeight(int n) {
int count = 0;
while (n > 0) {
if ((n & 1) == 1) count++;
n >>= 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 해당 문제를 풀 때 수학적인 방식과 비트 조작 방식으로 접근했었는데요, 리트코드 솔루션을 보니 Brian Kernighan's Algorithm 이라는 풀이법도 있더라구요! n & (n - 1) 연산을 이용해 오른쪽에서부터 1인 비트를 하나씩 지워가며 개수를 세는 방식으로, n에 포함된 1인 비트 개수만큼만 while 루프를 돌게 된다고 합니다.

참고 자료도 공유드려요~! ➡️ Link

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아. 끝까지 안가도 되는거군요. 링크까지 정말 감사합니다 !! 주말 잘보내세요!💛

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다 🙇🏻‍♀️ freejia 님도 좋은 주말 되세요~~!!

}
return count;
}
}
6 changes: 6 additions & 0 deletions valid-palindrome/std-freejia.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Solution {
public boolean isPalindrome(String s) {
String alphabetOnly = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
return new StringBuilder(alphabetOnly).reverse().toString().equals(alphabetOnly);
}
}