Skip to content

Commit 649b8a2

Browse files
authored
Merge pull request #1545 from ivanpenaloza/february11
adding algo
2 parents d8ef3be + dad91a2 commit 649b8a2

5 files changed

Lines changed: 90 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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
class Solution:
11+
12+
def hasPathSum(self, root: TreeNode, targetSum: int) -> bool:
13+
14+
if not root:
15+
return False
16+
if not root.left and not root.right and root.val == targetSum:
17+
return True
18+
else:
19+
temp_target = targetSum - root.val
20+
return self.hasPathSum(root.left, temp_target) \
21+
or self.hasPathSum(root.right, temp_target)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { TreeNode } from './TreeNode';
2+
3+
export function hasPathSum(root: TreeNode | null, targetSum: number): boolean {
4+
if (!root) {
5+
return false;
6+
}
7+
if (!root.left && !root.right && root.val === targetSum) {
8+
return true;
9+
}
10+
const tempTarget = targetSum - root.val;
11+
return hasPathSum(root.left, tempTarget) || hasPathSum(root.right, tempTarget);
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import unittest
2+
from typing import Optional, List
3+
from src.my_project.interviews.top_150_questions_round_22\
4+
.ex_73_path_sum import Solution, TreeNode
5+
6+
7+
class HasPathSumTestCase(unittest.TestCase):
8+
9+
def test_is_path_sum(self):
10+
solution = Solution()
11+
tree = TreeNode(1, TreeNode(2), TreeNode(3))
12+
output = solution.hasPathSum(root=tree, targetSum=3)
13+
self.assertTrue(output)
14+
15+
def test_is_no_path_sum(self):
16+
solution = Solution()
17+
tree = TreeNode(1, TreeNode(2), TreeNode(3))
18+
output = solution.hasPathSum(root=tree, targetSum=10)
19+
self.assertFalse(output)

0 commit comments

Comments
 (0)