Skip to content

Commit d616ef2

Browse files
committed
feat: 문제풀이 추가
1 parent ebd47a6 commit d616ef2

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// 시간 복잡도: O(n)
2+
// 공간 복잡도: O(n)
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* function TreeNode(val, left, right) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.left = (left===undefined ? null : left)
9+
* this.right = (right===undefined ? null : right)
10+
* }
11+
*/
12+
/**
13+
* @param {TreeNode} root
14+
* @param {number} k
15+
* @return {number}
16+
*/
17+
var kthSmallest = function(root, k) {
18+
const results = []
19+
20+
const dfs = (tree) => {
21+
results.push(tree.val)
22+
if (tree.left) dfs(tree.left)
23+
if (tree.right) dfs(tree.right)
24+
}
25+
26+
dfs(root)
27+
28+
return results[k-1]
29+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// 시간 복잡도: O(n^3)
2+
// 공간 복잡도: O(1)
3+
4+
/**
5+
* @param {string} s
6+
* @return {number}
7+
*/
8+
var countSubstrings = function(s) {
9+
let count = 0;
10+
11+
let plusIndex = 0;
12+
while (plusIndex !== s.length) {
13+
for (let i = 0 ; i < s.length - plusIndex; i++) {
14+
if (isValidSubstring(s, i, i + plusIndex)) count++
15+
}
16+
17+
plusIndex++;
18+
}
19+
20+
return count;
21+
};
22+
23+
24+
function isValidSubstring(s, left, right) {
25+
while (left <= right) {
26+
if (s[left] !== s[right]) return false;
27+
28+
left++;
29+
right--;
30+
}
31+
return true;
32+
}
33+
34+
console.log(countSubstrings("abc"));
35+
console.log(countSubstrings("aaa"));

0 commit comments

Comments
 (0)