-
Notifications
You must be signed in to change notification settings - Fork 126
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
[TONY] WEEK 02 Solutions #338
Changes from 2 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,23 @@ | ||
class Solution { | ||
private int i, p; | ||
public TreeNode buildTree(int[] preorder, int[] inorder) { | ||
// Time complexity: O(n) | ||
// Space complexity: O(n) | ||
return builder(preorder, inorder, Integer.MIN_VALUE); | ||
} | ||
|
||
private TreeNode builder(int[] preorder, int[] inorder, int stop) { | ||
if (p >= preorder.length) return null; | ||
if (inorder[i] == stop) { | ||
i += 1; | ||
return null; | ||
} | ||
|
||
TreeNode node = new TreeNode(preorder[p]); | ||
p += 1; | ||
|
||
node.left = builder(preorder, inorder, node.val); | ||
node.right = builder(preorder, inorder, stop); | ||
return node; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Solution { | ||
public int[] countBits(int n) { | ||
// time complexity: O(n) | ||
// space complexity: O(n) | ||
int[] output = new int[n+1]; | ||
int num = 0; | ||
while (num <= n) output[num] = Integer.bitCount(num++); | ||
return output; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution { | ||
public int numDecodings(String s) { | ||
// time complexity: O(n) | ||
// space complexity: O(n) | ||
if (s.charAt(0) == '0') return 0; | ||
|
||
int[] dp = new int[s.length() + 1]; | ||
dp[0] = 1; | ||
dp[1] = 1; | ||
|
||
for (int i = 2; i <= s.length(); i++) { | ||
int oneDigit = Integer.parseInt(s.substring(i-1, i)); | ||
int twoDigits = Integer.parseInt(s.substring(i-2, i)); | ||
|
||
if (oneDigit > 0 && oneDigit < 10) dp[i] += dp[i-1]; | ||
if (twoDigits >= 10 && twoDigits <= 26) dp[i] += dp[i-2]; | ||
} | ||
|
||
return dp[s.length()]; | ||
} | ||
} |
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. 아하핫.... 출제 의도대로 풀려면 어떻게 하는게 좋을지 조금 더 구체적인 조언 부탁드려도 괜찮을까요? 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. 현재 작성해주신 코드는 인코딩 결과가 독립적이지 않습니다. 메모리(Map)를 거쳐 encode/decode를 진행하도록 구현되어있는데 encode에서는 원본 문자열의 내용이 복원될 수 있는 형태로 가공해야 하고, 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. 안녕하세요. 해당 방법에는 str이 " " 와 같이 공백일경우 통과되지 못하는 케이스가 발생됩니다. 이 문제는 공백이 아닌 다른 문자로 split을 시도하여도 이슈가 발생됩니다. 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. @dev-jonghoonpark 님, 피드백이 너무 좋네여! 🫰 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. @DaleSeo @dev-jonghoonpark @taekwon-dev |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
public class Solution { | ||
// time complexity: O(n) | ||
// space complexity: O(n) | ||
Map<Integer, String> encode = new HashMap<>(); | ||
/* | ||
* @param strs: a list of strings | ||
* @return: encodes a list of strings to a single string. | ||
*/ | ||
public String encode(List<String> strs) { | ||
// write your code here | ||
int key = 0; | ||
for (String str : strs) encode.put(key++, str); | ||
return String.valueOf(key); | ||
} | ||
|
||
/* | ||
* @param str: A string | ||
* @return: decodes a single string to a list of strings | ||
*/ | ||
public List<String> decode(String str) { | ||
// write your code here | ||
List<String> output = new ArrayList<>(); | ||
int decode = 0; | ||
while (decode < Integer.valueOf(str)) output.add(encode.get(decode++)); | ||
return output; | ||
} | ||
} |
leokim0922 marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution { | ||
public boolean isAnagram(String s, String t) { | ||
// time complexity: O(n log n) | ||
// space complexity: O(n) | ||
if (s.length() != t.length()) return false; | ||
|
||
char[] sArr = s.toCharArray(); | ||
char[] tArr = t.toCharArray(); | ||
|
||
Arrays.sort(sArr); | ||
Arrays.sort(tArr); | ||
|
||
return Arrays.equals(sArr, tArr); | ||
} | ||
} |
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.
해당 숫자의 bit에서 1의 갯수를 구해주는 내장 함수입니다ㅎㅎ
잘 쓰시면 도움 되실꺼에요. 시간 복잡도는 O(1) 입니다!