-
-
Notifications
You must be signed in to change notification settings - Fork 247
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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]; | ||
} | ||
} | ||
return dp[sLen]; | ||
} | ||
} |
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; | ||
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. 저도 해당 문제를 풀 때 수학적인 방식과 비트 조작 방식으로 접근했었는데요, 리트코드 솔루션을 보니 Brian Kernighan's Algorithm 이라는 풀이법도 있더라구요! 참고 자료도 공유드려요~! ➡️ Link 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. 아. 끝까지 안가도 되는거군요. 링크까지 정말 감사합니다 !! 주말 잘보내세요!💛 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. 감사합니다 🙇🏻♀️ freejia 님도 좋은 주말 되세요~~!! |
||
} | ||
return count; | ||
} | ||
} |
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); | ||
} | ||
} |
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.
1차원 dp 리스트를 이용한 O(n) space DP로 풀이하셨네요!
dp[i]
를 계산할 때 매 단계에서dp[i - 1]
과dp[i - 2]
만 확인하기 때문에, 이 값들을 두 개의 변수로 트래킹한다면 O(1) space DP로 최적화를 한 번 더 할 수 있을 것 같습니다~! 😀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.
오! 변수로 트래킹 하는 방법도 있네요. 재밌을 것 같아요. 피드백 감사합니다 !!