Skip to content

Commit 865e77b

Browse files
committed
two pointers
1 parent cd0d2d0 commit 865e77b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

twopointers.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def isPalindrome(str: str) -> bool:
2+
l, r = 0, len(str) - 1
3+
while l < r:
4+
if str[l] != str[r]:
5+
return False
6+
l += 1
7+
r -= 1
8+
return True
9+
def sortedTwoSum(arr: list[int], target: int) -> tuple[int]:
10+
l, r = 0, len(arr) - 1
11+
while l < r:
12+
if arr[l] + arr[r] == target: return (l, r)
13+
if arr[l] + arr[r] > target:
14+
r -= 1
15+
else: l += 1
16+
return (-1, -1)
17+
18+
print(isPalindrome("asddsa")) #True
19+
print(isPalindrome("asdsa")) #True
20+
print(isPalindrome("ab")) #False
21+
print(isPalindrome("")) #True
22+
23+
print(sortedTwoSum([-1,2,3,4,5,7,8], 8)) # (2, 4)

0 commit comments

Comments
 (0)