Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:

answer = dict()

for k, v in enumerate(nums):

if v in answer:
return [answer[v], k]
else:
answer[target - v] = k

return []
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod
import re

class Solution:
def isPalindrome(self, s: str) -> bool:

# To lowercase
s = s.lower()

# Remove non-alphanumeric characters
s = re.sub(pattern=r'[^a-zA-Z0-9]', repl='', string=s)

# Determine if s is palindrome or not
len_s = len(s)

for i in range(len_s//2):

if s[i] != s[len_s - 1 - i]:
return False

return True
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod

class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right

class Solution:

def hasPathSum(self, root: TreeNode, targetSum: int) -> bool:

if not root:
return False
if not root.left and not root.right and root.val == targetSum:
return True
else:
temp_target = targetSum - root.val
return self.hasPathSum(root.left, temp_target) \
or self.hasPathSum(root.right, temp_target)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { TreeNode } from './TreeNode';

export function hasPathSum(root: TreeNode | null, targetSum: number): boolean {
if (!root) {
return false;
}
if (!root.left && !root.right && root.val === targetSum) {
return true;
}
const tempTarget = targetSum - root.val;
return hasPathSum(root.left, tempTarget) || hasPathSum(root.right, tempTarget);
}
19 changes: 19 additions & 0 deletions tests/test_150_questions_round_22/test_73_path_sum_round_22.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import unittest
from typing import Optional, List
from src.my_project.interviews.top_150_questions_round_22\
.ex_73_path_sum import Solution, TreeNode


class HasPathSumTestCase(unittest.TestCase):

def test_is_path_sum(self):
solution = Solution()
tree = TreeNode(1, TreeNode(2), TreeNode(3))
output = solution.hasPathSum(root=tree, targetSum=3)
self.assertTrue(output)

def test_is_no_path_sum(self):
solution = Solution()
tree = TreeNode(1, TreeNode(2), TreeNode(3))
output = solution.hasPathSum(root=tree, targetSum=10)
self.assertFalse(output)