Skip to content

Commit

Permalink
Merge pull request #349 from f-exuan21/main
Browse files Browse the repository at this point in the history
[์•„ํ˜„] WEEK02 Solutions
  • Loading branch information
f-exuan21 authored Aug 25, 2024
2 parents 3e1d624 + bfdfbaa commit 19e7562
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/

// time : O(n)
// space : O(n)
// n์€ ํŠธ๋ฆฌ ๋…ธ๋“œ ์ˆ˜

class Solution {

private int i = 0;
Map<Integer, Integer> map = new HashMap<>();

public TreeNode buildTree(int[] preorder, int[] inorder) {

for(int i = 0; i < inorder.length; i++) {
map.put(inorder[i], i);
}

return build(preorder, inorder, 0, inorder.length);

}

private TreeNode build(int[] preorder, int[] inorder, int start, int end) {
if(i >= preorder.length || start >= end) {
return null;
}

int value = preorder[i++];
int index = map.get(value);

TreeNode leftTreeNode = build(preorder, inorder, start, index);
TreeNode rightTreeNode = build(preorder, inorder, index+1, end);

return new TreeNode(value, leftTreeNode, rightTreeNode);
}

}
18 changes: 18 additions & 0 deletions counting-bits/f-exuan21.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//time : O(n)
//space : O(n)

class Solution {

// f(n) = f(n >> 1) + (n & 1)

public int[] countBits(int n) {
int[] answer = new int[n + 1];

answer[0] = 0;
for(int i = 1; i <= n; i++) {
answer[i] = answer[i >> 1] + (i & 1);
}

return answer;
}
}
24 changes: 24 additions & 0 deletions valid-anagram/f-euxan21.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// time : O(n)
// space : O(1)

class Solution {
public boolean isAnagram(String s, String t) {

if(s.length() != t.length()) return false;

int[] charCount = new int[26];

for(int i = 0; i < s.length(); i++) {
charCount[s.charAt(i) - 'a']++;
charCount[t.charAt(i) - 'a']--;
}

for(int i : charCount) {
if(i != 0) {
return false;
}
}

return true;
}
}

0 comments on commit 19e7562

Please sign in to comment.