Skip to content

Commit abcbb60

Browse files
committed
adding algo
1 parent b123ad4 commit abcbb60

5 files changed

Lines changed: 159 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def twoSum(self, nums: List[int], target: int) -> List[int]:
6+
7+
answer = dict()
8+
9+
for k, v in enumerate(nums):
10+
11+
if v in answer:
12+
return [answer[v], k]
13+
else:
14+
answer[target - v] = k
15+
16+
return []
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
import re
4+
5+
class Solution:
6+
def isPalindrome(self, s: str) -> bool:
7+
8+
# To lowercase
9+
s = s.lower()
10+
11+
# Remove non-alphanumeric characters
12+
s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s)
13+
14+
# Determine if s is palindrome or not
15+
len_s = len(s)
16+
17+
for i in range(len_s//2):
18+
19+
if s[i] != s[len_s - 1 - i]:
20+
return False
21+
22+
return True
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class TreeNode:
5+
def __init__(self, val=0, left=None, right=None):
6+
self.val = val
7+
self.left = left
8+
self.right = right
9+
10+
11+
12+
class BSTIterator:
13+
14+
def __init__(self, root: Optional[TreeNode]):
15+
self.stack = []
16+
self._push_left(root)
17+
18+
def _push_left(self, node: Optional[TreeNode]) -> None:
19+
"""Push all left children of a node onto the stack."""
20+
while node:
21+
self.stack.append(node)
22+
node = node.left
23+
24+
def next(self) -> int:
25+
# Pop the top node (next smallest element)
26+
node = self.stack.pop()
27+
28+
# If it has a right child, push all left descendants of the right child
29+
if node.right:
30+
self._push_left(node.right)
31+
32+
return node.val
33+
34+
def hasNext(self) -> bool:
35+
return len(self.stack) > 0
36+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { TreeNode } from './TreeNode';
2+
3+
class BSTIterator {
4+
private stack: TreeNode[];
5+
6+
constructor(root: TreeNode | null) {
7+
this.stack = [];
8+
this.pushLeft(root);
9+
}
10+
11+
private pushLeft(node: TreeNode | null): void {
12+
/**
13+
* Push all left children of a node onto the stack.
14+
*/
15+
while (node !== null) {
16+
this.stack.push(node);
17+
node = node.left;
18+
}
19+
}
20+
21+
next(): number {
22+
// Pop the top node (next smallest element)
23+
const node = this.stack.pop()!;
24+
25+
// If it has a right child, push all left descendants of the right child
26+
if (node.right !== null) {
27+
this.pushLeft(node.right);
28+
}
29+
30+
return node.val;
31+
}
32+
33+
hasNext(): boolean {
34+
return this.stack.length > 0;
35+
}
36+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import unittest
2+
from typing import Optional, List
3+
from src.my_project.interviews.top_150_questions_round_22\
4+
.ex_76_binary_search_tree_iterator import BSTIterator, TreeNode
5+
6+
7+
class BSTIteratorTestCase(unittest.TestCase):
8+
9+
def test_example_1(self):
10+
"""
11+
Input: ["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"]
12+
[[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
13+
Output: [null, 3, 7, true, 9, true, 15, true, 20, false]
14+
15+
Explanation:
16+
BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]);
17+
bSTIterator.next(); // return 3
18+
bSTIterator.next(); // return 7
19+
bSTIterator.hasNext(); // return True
20+
bSTIterator.next(); // return 9
21+
bSTIterator.hasNext(); // return True
22+
bSTIterator.next(); // return 15
23+
bSTIterator.hasNext(); // return True
24+
bSTIterator.next(); // return 20
25+
bSTIterator.hasNext(); // return False
26+
"""
27+
# Build tree: [7, 3, 15, null, null, 9, 20]
28+
# 7
29+
# / \
30+
# 3 15
31+
# / \
32+
# 9 20
33+
root = TreeNode(7)
34+
root.left = TreeNode(3)
35+
root.right = TreeNode(15)
36+
root.right.left = TreeNode(9)
37+
root.right.right = TreeNode(20)
38+
39+
bst_iterator = BSTIterator(root)
40+
41+
self.assertEqual(bst_iterator.next(), 3)
42+
self.assertEqual(bst_iterator.next(), 7)
43+
self.assertTrue(bst_iterator.hasNext())
44+
self.assertEqual(bst_iterator.next(), 9)
45+
self.assertTrue(bst_iterator.hasNext())
46+
self.assertEqual(bst_iterator.next(), 15)
47+
self.assertTrue(bst_iterator.hasNext())
48+
self.assertEqual(bst_iterator.next(), 20)
49+
self.assertFalse(bst_iterator.hasNext())

0 commit comments

Comments
 (0)